Space between UITableView Cells
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;
}
Magnus
Updated on October 15, 2020Comments
-
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 about 12 yearsAlready tried this. This just changes the height for each cell, not distance between cells
-
kamprath about 12 yearsYou should create cell layouts that have the padding and appearance you are looking for.
-
bneely about 12 yearsThis method is common practice for the problem you described. If you already tried it, you should mention it in the original question.
-
Magnus about 12 yearsthat's what I ended up doing. Thanks man!
-
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 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 almost 11 yearsI 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 over 10 yearsI 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 over 10 years@estobbart Edited Thanks.
-
Zorayr over 10 yearsThe 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.