2013-08-24 44 views
2

當用戶點擊單元格時,我想更新我的UITableView;包括這個竊聽單元格的內容。最簡單的方法是更新內部參數,然後調用[self.tableView reloadData];如何在不停止單元格選擇動畫的情況下重新加載UITableView

但是,reloadData立即停止了我的抽頭單元格的漂亮的藍色 - >無選擇動畫。

是否有(標準)方式更新我的表格單元而不停止點擊單元格的動畫?

注意在這種情況下,我不添加或刪除單元格;我只想改變內容(例如,啓動活動指示器,或更改標籤的顏色)。

回答

1

在您的情況下,您只需獲取指向所有可見單元格的指針並更新它們即可。事情是這樣的:

- (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; 
} 
+0

大,感謝的人!但它確實意味着將單元格內容創建代碼從'cellForRowAtIndexPath'移開,但無論如何這是一件好事。 –

+1

很高興幫助:) –

+0

通常用於在cellForRowAtIndexPath方法中創建性能單元格的內容if(cell == nil){... // here; }。而willDisplayCell方法只是可以更新一些東西,例如停止一些活動指示器 –

相關問題