简体   繁体   English

如何在iOS中的Tableview单元格之间设置空间?

[英]How to Set a Space between Tableview Cell in iOS?

I am newbie in iOS development and i know this question is asked many times but i confuse for it. 我是iOS开发的新手,我知道这个问题被问过很多次,但我对此感到困惑。 I want to know how to set a space between UITableview cell in section.In my app UITableview contain two section first section contain only one data value so not any problem. 我想知道如何在section中的UITableview单元之间设置空间。在我的应用程序UITableview中包含两个部分,第一部分仅包含一个数据值,因此没有任何问题。 but my second section contain 5 to 7 data value But not a space between them how to set a space in footer between second section cell in UITableview. 但是我的第二部分包含5到7个数据值,但它们之间没有空格如何在UITableview的第二部分单元格之间的页脚中设置空格。

OK so you have two methods for sections 好,所以您有两种方法

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

and

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

but neither serve the purpose of putting space between cells in a single section. 但两者都不是为了将单元格之间的空间放在单个区域中。

For my money there are two options... first and most complicated / hacky 为了我的钱,有两种选择...第一种也是最复杂的/ hacky

  1. in - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you could return the count of the cells in the second section and in cellForRowAtIndexPath if (section == 0) put the data in the usual way... else you can pull the info out of the array or whatever using [theArray objectAtIndex:indexPath.section] instead. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView中, if (section == 0)以通常的方式放置数据,则可以返回第二部分和cellForRowAtIndexPath中的单元格计数... else您可以提取信息在数组之外或使用[theArray objectAtIndex:indexPath.section]代替。

  2. (much simpler, and better practice).. Create a UITableViewCell subclass and use that in your cellForRowAtIndexPath like so (更简单,更好的做法)。创建一个UITableViewCell子类,并像这样在您的cellForRowAtIndexPath使用它

     (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; static NSString *MyCellIdentifier = @"MyCustomCell"; switch (indexPath.section) { case 0: { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; // setup your 1st section cells here return cell; } default: { MYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier]; if (!cell) cell = [[MYTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier]; // all other layout code here if indexPath.row == 0 etc. return cell; } } 

in your custom cell .m you can set 在您的自定义单元.m中,您可以设置

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,0,0); // lay them out at the top / middle / wherever  in your cell
    self.textLabel.frame = CGRectMake(0,0,0,0); // which will allow for some space at the bottom / edges
}

then finally use 然后最后使用

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   if(indexPath.section == 0)
     return THE_CELL_HEIGHT;
   else 
     return THE_CELL_HEIGHT + PADDING;
}

This way you can set padding in the cell itself, which is cleaner and reusable. 这样,您可以在单元格本身中设置填充,这样更干净且可重复使用。 If you want different colours to mark the spacing, you should create UIViews in the custom UITableViewCell subclass method 如果要用不同的颜色标记间距,则应在自定义UITableViewCell子类方法中创建UIViews

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

and add them using [self.contentView addSubView:someView]; 并使用[self.contentView addSubView:someView];添加它们[self.contentView addSubView:someView]; you can always set the backgroundColor of a cell to [UIColor clearColor]; 您始终可以将单元格的backgroundColor设置为[UIColor clearColor]; if you have an image / content behind the tableView 如果您在tableView后面有图像/内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM