【问题标题】:UITableViewController - cells size separator and backgroundcolorUITableViewController - 单元格大小分隔符和背景色
【发布时间】:2015-04-13 05:49:06
【问题描述】:

我正在尝试为我的 UITableViewController 实现 airbnb 外观。

  1. 分隔符不在屏幕的整个宽度内
  2. 每个单元格都有唯一的大小和背景颜色

我已经设法使用静态表解决 #2 并在 IB 中设置每个单元格大小,但存在以下问题:

  1. 没有在 IB 中设置背景颜色(更改了背景颜色和色调)
  2. 当单元格没有内容但只有返回单元格高度的函数- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath时,我希望以编程方式“删除”单元格 对所有单元格都是通用的并且在向单元格填充数据之前调用,所以在这种方法中我无法决定是否应该删除单元格。

【问题讨论】:

  • 您不能真正从静态UITableView 中删除单元格,因为您没有实现cellForRow...。查看THIS 问题了解更多信息。

标签: ios objective-c uitableview


【解决方案1】:

我认为你应该使用表视图的委托方法willDisplayCell: forRowAtIndexPath:。在单元格准备好在表格视图上显示之前调用此委托方法。在此委托中,您将创建单元格,如果您需要进行一些修改,可以在此处进行,这将在单元格显示之前反映。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor redColor]];
    // and all other cell customizations 
}

【讨论】:

    【解决方案2】:
    1. 您可以在布局子视图之前设置表格视图的 separatorInset 和 layoutMargins 属性,在显示单元格之前设置单元格的 separatorInset 和 layoutMargins 属性。

    2. 要设置自定义单元格的高度,您可能会通过调用表格视图数据源方法而不是调用 cellForRowAtIndexPath: 表格视图方法来获得所需的单元格。

    例子:

    // 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
    }
    

    【讨论】:

    • 感谢您编写如此详细的解决方案,不幸的是它仍然存在相同的基本调用顺序问题,我的表数据在视图加载后被填充,因此当时函数 'heightForRowAtIndexPath' , 'willDisplayCell' 等被称为我的数据为零,无法决定单元格是否“删除”
    • 您可以添加检查数据状态: if (!data) { return; } 在这些方法的开头并在数据初始化后立即调用 [tableView reloadData]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2015-11-29
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多