简体   繁体   English

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

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

I have a variable that is going to keep track of how many cells need to be colored. 我有一个变量,可以跟踪需要着色的细胞数量。 So if that variable is 3, then the top three cells backgroundcolor will change. 因此,如果该变量为3,那么前三个单元格backgroundcolor将会改变。 How can I do this? 我怎样才能做到这一点?

I know I need to update this in 我知道我需要更新它

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

But how can I ensure the top cells have a different background color based on my variable? 但是如何根据我的变量确保顶部单元格具有不同的背景颜色?

The indexPath parameter is your starting point. indexPath参数是您的起点。 If coloredCells is an integer that holds the number of cells you are coloring, your method would include something like 如果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;
}

Now, if you adjust the value of coloredCells , you'll need to inform the table view that some of its views have changed. 现在,如果调整coloredCells的值, coloredCells需要通知表视图其某些视图已更改。 The laziest way to do that is to reload the whole table: 最懒的方法是重新加载整个表:

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

Or you can take a little more effort to reload just the cells that have colored backgrounds: 或者您可以花更多的精力重新加载具有彩色背景的单元格:

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];

You would test for the row number and change the color accordingly. 您将测试行号并相应地更改颜色。 In cellForRowAtIndexPath use: 在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

You can use the tableView:willDisplayCell:forRowAtIndexPath: of your UITableViewDelegate to change things like the background colour of the cell. 您可以使用UITableViewDelegate的tableView:willDisplayCell:forRowAtIndexPath:来更改单元格的背景颜色等内容。 Changes made in cellForRowAtIndexPath may be lost before the cell is actually displayed, so it's usually better to do it in this method. 在实际显示单元格之前,cellForRowAtIndexPath中所做的更改可能会丢失,因此通常最好在此方法中执行此操作。

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

From the discussion of this method in the reference: 从参考中对此方法的讨论:

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. 表视图在使用单元格绘制行之前将此消息发送到其委托,从而允许委托在显示单元格对象之前对其进行自定义。 This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. 此方法为委托提供了覆盖表视图前面设置的基于状态的属性的机会,例如选择和背景颜色。 After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out. 委托返回后,表视图仅设置alpha和frame属性,然后仅在向行或向外滑动时为行设置动画。

No, you don't have to update the tableView:cellForRowRowAtIndexPath: delegate method. 不,您不必更新tableView:cellForRowRowAtIndexPath:委托方法。 All you have to do is this: 你所要做的就是:

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

Note that calling tableView:cellForRowAtIndexPath: from type id<UITableViewDataSource> is different from calling cellForRowAtIndexPath: from type UITableView . 请注意,调用tableView:cellForRowAtIndexPath:从类型id<UITableViewDataSource>是从调用不同cellForRowAtIndexPath:从类型UITableView The former calls the delegate method (this should never be directly called) and the latter returns the current cell for an index path without recalculating the cell . 前者调用委托方法(永远不应该直接调用),后者返回索引路径的当前单元格而不重新计算单元格

If you only have one section in your table view, the math to calculate the top n cells is easy. 如果表视图中只有一个部分,则计算前n个单元格的数学运算很容易。 If your "variable that is going to keep track of how many cells need to be colored" is (NSUInteger)numberOfHighlightedCells , this is some simple loop code your could run: 如果你的“要跟踪需要着色多少个单元格的变量”是(NSUInteger)numberOfHighlightedCells ,这是一个你可以运行的简单循环代码:

NSUInteger i;

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

However, if you have more than one section in your table, some very complicated calculating of index paths may be necessary. 但是,如果表中有多个部分,则可能需要对索引路径进行一些非常复杂的计算。

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

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