2012-07-22 42 views
0

我有一個tableView,最初有4個部分,每個部分有1行。根據用戶與第一行的交互情況,我需要在該部分插入或刪除第二行。之後,我需要進行單元的設置,這是在點擊後的下一個單元。這裏是我處理交互的代碼:UITableViewCell在insertRowsAtIndexPaths後爲零

-(void) tableViewSwitchToggled: (UISwitch*) sender{ 
    targetSection = sender.tag; //each switch is added to cell's contentview and it's tag is set to indexPath.section 
    UITableView* tableView = ... 

    if (someBool){ 
     [tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 1 inSection: targetSection]] withRowAnimation: UITableViewRowAnimationTop]; 
     UITableViewCell* detailsCell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: targetSection + 1]]; //I try to get the first row of the next section and it is always nil!!! 
     //do something with detailsCell content 
    } 
    else{ 
     [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 1 inSection: targetSection]] withRowAnimation: UITableViewRowAnimationRight]; 
     UITableViewCell* detailsCell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: targetSection + 1]]; //and it works perfectly, giving me the cell address 
     //do something with detailsCell content 
    } 
} 

所以這裏是我的問題 - 爲什麼我插入一行後得到一個零單元格?不是tableView應該已經分配了所有單元格嗎?謝謝你的幫助!

回答

1

您只能獲得對可見的單元格的引用。你可以向tableView提供一個可見的單元格數組(或者它們的索引路徑,忘記它們)。細胞是一種在可見時存在的暫時性物質 - 這就是爲什麼您可以回收它們的原因。

你需要做的是在代碼中注意當這個單元格重新出現時,你想要做一些特殊的事情。

+0

感謝您的幫助。最終我能夠通過將其中一個代碼部分放在'tableView:willDisplayCell:forRowAtIndexPath:'方法 – NikGreen 2012-07-22 17:26:01

+0

中來解決我的問題。正是我所說的。當你想改變它的時候,這個單元並不存在,而是推遲到它看來解決了問題。 – 2012-07-22 18:22:54

相關問題