Space between UITableView Cells

34,179

Solution 1

Based on the follow-up in comments above, it sounds like you should use the UITableView backgroundView property to set your custom background image, then use a grouped table view with one cell per section. To me, this sounds like the quickest and easiest way to implement the type of UI you're describing.

Relevant portions of UITableView API documentation:

@property(nonatomic, readwrite, retain) UIView *backgroundView
...
- (NSInteger)numberOfRowsInSection:(NSInteger)section

And UITableViewDataSource protocol:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Solution 2

one alternate solution of this problem is just implement the method tableview:heightForFooterInSection like

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 5;
}

And then implement the tableView:ViewForFooterInSection like below //Returns a transparent footer actually.

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ 

    UIView *view = [[UIView alloc]init]; 
    [view setAlpha:0.0F]; 
    return view; 

}
Share:
34,179
Magnus
Author by

Magnus

Updated on October 15, 2020

Comments

  • Magnus
    Magnus over 3 years

    I have a UITableView which contains x amount of cells. However, I want to have 20 px distance between each cell. My tableview only contains one section.

    I have googled around and searched the forum, but I couldn't find anything that works for my purpose.

    Is it possible to set content offset to the cell, or the imageview inside the cell?

    Can someone please help me?

  • Magnus
    Magnus about 12 years
    Already tried this. This just changes the height for each cell, not distance between cells
  • kamprath
    kamprath about 12 years
    You should create cell layouts that have the padding and appearance you are looking for.
  • bneely
    bneely about 12 years
    This method is common practice for the problem you described. If you already tried it, you should mention it in the original question.
  • Magnus
    Magnus about 12 years
    that's what I ended up doing. Thanks man!
  • TigerCoding
    TigerCoding about 12 years
    @bneely OP explained what he wanted quite well, "...I want to have 20 px distance between each cell. My tableview only contains one section." Notice the between each cell, not cell height.
  • kamprath
    kamprath about 12 years
    @Javy You have to manually create the aesthetic of distance between each cell by setting cell height and then creating the appearance of margins and padding with the cell such that you get the desired look of distance between each cell.
  • MLQ
    MLQ almost 11 years
    I think this is a pretty bad solution if you need to present a single array as a single section with its own section header, and another array in another section with another section header.
  • estobbart
    estobbart over 10 years
    I ended up using this with a transparent footer. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[[UIView alloc] init] autorelease]; [view setAlpha:0.0F]; return view; }
  • Gyanendra Singh
    Gyanendra Singh over 10 years
    @estobbart Edited Thanks.
  • Zorayr
    Zorayr over 10 years
    The only downside of this is it adds extra space between the sections, and not between the rows. And if you do need to group rows in several sections, then this solution does not work.