簡體   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