在您的情況下,您只需獲取指向所有可見單元格的指針並更新它們即可。事情是這樣的:
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSArray* visibleCells = [tableView indexPathsForVisibleRows];
for (NSIndexPath* indexPath in visibleCells)
{
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath]; // Your method, which updates content...
}
}
如果要更新其他單元格的內容,你可以使用這樣的事情:
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
[self updateCell:cell atIndexPath:indexPath]; // Your method, which updates content...
}
所以你的電池將始終顯示正確的內容。
關於創建內容:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* CellIdentifier = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[self createContentForCell:cell atIndexPath:indexPath]; // so here to create content or customize cell
}
return cell;
}
大,感謝的人!但它確實意味着將單元格內容創建代碼從'cellForRowAtIndexPath'移開,但無論如何這是一件好事。 –
很高興幫助:) –
通常用於在cellForRowAtIndexPath方法中創建性能單元格的內容if(cell == nil){... // here; }。而willDisplayCell方法只是可以更新一些東西,例如停止一些活動指示器 –