繁体   English   中英

如何动态更改UITableViewCell的背景颜色?

[英]How can I change the background color of a UITableViewCell dynamically?

我有一个变量,可以跟踪需要着色的细胞数量。 因此,如果该变量为3,那么前三个单元格backgroundcolor将会改变。 我怎样才能做到这一点?

我知道我需要更新它

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

但是如何根据我的变量确保顶部单元格具有不同的背景颜色?

indexPath参数是您的起点。 如果coloredCells是一个整数,它保存着色的单元格数,那么你的方法将包括类似的东西

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    // fetch or create the cell, first
    UITableViewCell *cell = // ... 

    // then set it up
    if(indexPath.row < self.coloredCells) {
        cell.contentView.backgroundColor = [UIColor redColor];
    } else {
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }

    // perform rest of cell setup
    // ...

    return cell;
}

现在,如果调整coloredCells的值, coloredCells需要通知表视图其某些视图已更改。 最懒的方法是重新加载整个表:

// elsewhere...
self.coloredCells = 4;
[self.tableView reloadData];

或者您可以花更多的精力重新加载具有彩色背景的单元格:

self.coloredCells = newColoredCount;
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:newColoredCount];
for(int i = 0; i < newColoredCount; i++) {
    [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];

您将测试行号并相应地更改颜色。 在cellForRowAtIndexPath中使用:

 //having allocated or recycled a cell into myCell pointer above
 //test for row and assign background color like so

if (indexPath.row < 3) {
  myCell.contentView.backgroundColor = [UIColor greenColor];
 } else {
  myCell.contentView.backgroundColor = [UIColor redColor];
 }

 //continue configuring your cell

您可以使用UITableViewDelegate的tableView:willDisplayCell:forRowAtIndexPath:来更改单元格的背景颜色等内容。 在实际显示单元格之前,cellForRowAtIndexPath中所做的更改可能会丢失,因此通常最好在此方法中执行此操作。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row < numberOfColoredRows) {
        cell.backgroundColor = [UIColor redColor];
    }
}

从参考中对此方法的讨论:

表视图在使用单元格绘制行之前将此消息发送到其委托,从而允许委托在显示单元格对象之前对其进行自定义。 此方法为委托提供了覆盖表视图前面设置的基于状态的属性的机会,例如选择和背景颜色。 委托返回后,表视图仅设置alpha和frame属性,然后仅在向行或向外滑动时为行设置动画。

不,您不必更新tableView:cellForRowRowAtIndexPath:委托方法。 你所要做的就是:

[self.tableView cellForRowAtIndexPath:indexPath].backgroundColor = desiredUIColor;

请注意,调用tableView:cellForRowAtIndexPath:从类型id<UITableViewDataSource>是从调用不同cellForRowAtIndexPath:从类型UITableView 前者调用委托方法(永远不应该直接调用),后者返回索引路径的当前单元格而不重新计算单元格

如果表视图中只有一个部分,则计算前n个单元格的数学运算很容易。 如果你的“要跟踪需要着色多少个单元格的变量”是(NSUInteger)numberOfHighlightedCells ,这是一个你可以运行的简单循环代码:

NSUInteger i;

for (i = 0; i < numberOfHighlightedCells; i++) {
    [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].backgroundColor = desiredUIColor;
}

但是,如果表中有多个部分,则可能需要对索引路径进行一些非常复杂的计算。

暂无
暂无

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

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