简体   繁体   English

UITableViewController-单元格大小分隔符和backgroundcolor

[英]UITableViewController - cells size separator and backgroundcolor

I'm trying to achieve airbnb look for my UITableViewController. 我正在尝试为我的UITableViewController实现airbnb外观。

  1. separators are not in the full width of the screen 分隔符不在屏幕的整个宽度上
  2. each cell has a unique size and background color 每个单元格都有独特的大小和背景色

I've managed to tackle #2 using static table and setting each cell size in IB but have the following problems: 我设法使用静态表解决#2并在IB中设置每个单元格大小,但存在以下问题:

  1. setting a background color in IB didn't take (changed both background color as well as tint) 在IB中设置背景颜色没有用(更改了背景颜色和色调)
  2. I wish to "delete" a cell programmatically when it has no content but the only function that returns cell height - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath is generic for all cells and is called prior to populating the cells with data, so at this method I can't decide if the cell should be deleted or not. 我希望以编程方式“删除”一个没有内容但只有一个返回单元格高度的函数的单元格- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath对于所有单元格都是通用的, 并且在用数据填充单元格,因此在这种方法下,我无法决定是否应删除单元格。

I think you should use the delegate method willDisplayCell: forRowAtIndexPath: of the table view. 我认为您应该使用表视图的委托方法willDisplayCell: forRowAtIndexPath: This delegate method is called just before the cell is ready to be displayed on the table view. 在单元格准备好在表视图上显示之前,将调用此委托方法。 In this delegate you will have the cell created, if you need some modifications you can do here, which will be reflected just before the cell is displayed. 在此委托中,您将创建单元格,如果需要一些修改,可以在此处进行,这将在显示单元格之前反映出来。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor redColor]];
    // and all other cell customizations 
}
  1. You could set separatorInset and layoutMargins properties of table view before layout subviews and cell's separatorInset and layoutMargins properties before displaying cell. 您可以在布局子视图之前设置表格视图的spacerInset和layoutMargins属性,在显示单元格之前设置单元格的spacerInset和layoutMargins属性。

  2. For setting custom cell's height you may get needed cell by calling a table view data source method instead of call cellForRowAtIndexPath: method of table view. 要设置自定义单元格的高度,您可以通过调用表视图数据源方法而不是调用表视图的cellForRowAtIndexPath:方法来获得所需的单元格。

Example: 例:

// 1. Make full width separators:
- (void)viewWillLayoutSubviews {
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - Table view data source
// 2.Customize cells background color and height
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = [NSString stringWithFormat:@"s%ld-r%ld", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }

    if (indexPath.row % 2) {
        cell.backgroundColor = [UIColor redColor];
        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.textLabel.text = cellIdentifier;
    } else {
        cell.backgroundColor = [UIColor cyanColor];
        cell.contentView.backgroundColor = [UIColor whiteColor];//background color of contentView overlaps cell's background color
    }

    return cell;
}


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

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; //getting cell at indexPath
    if ([self isCellEmpty:cell]) {
        return 22;
    }
    return 44;
}

- (BOOL)isCellEmpty:(UITableViewCell *)cell {
    return !cell.textLabel.text.length; //Place your code for checking cell's content
}

在此处输入图片说明

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

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