2013-05-29 40 views
1

我已經在故事板中設置了一個原型單元格的tableview。 現在我想編輯單元格的子視圖,如果它被滑過,所以我實現了委託方法tableView:willBeginEditingRowAtIndexPath :. 如何從此方法中獲取正在編輯的當前單元格?如果我使用tableView:cellForRowAtIndexPath:我得到一個新的單元格,而不是我需要的單元格,因爲隨後調用dequeueReusableCellWithIdentifier:forIndexPath:似乎爲相同的標識符和indexPath返回不同的對象。如何在UITableView中安全地調用tableView:cellForRowAtIndexPath:安全?

我可以用下面的代碼很容易重現此問題:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    NSLog(@"cell = %@", cell); 
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    NSLog(@"different cell = %@", cell); 
} 

所以,當我不能使用這種方法,我怎麼被放置在特定indexPath當前單元格? 我使用的是iOS 6

+2

隨後調用'dequeueReusableCellWithIdentifier'將返回不同的單元格,因爲這就是這個方法的作用:它基本上是'爲我創建一個新單元格,或者如果它不再被使用,給我一箇舊單元格'。只要單元格可見,'cellForRowAtIndexPath'將始終返回相同的單元格。如果您再次滾動並返回,則以前使用的單元格可能已被重新用於另一行。 – Vegar

+0

謝謝你Vegar。我的問題是我混淆了表視圖的委託tableView:cellForRowAtIndexPath:與表視圖的方法cellForRowAtIndexPath :. – user1169629

回答

4

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

UITableView

+0

大聲笑謝謝。首先,我沒有得到它,現在我看到我直接打電話給委託人,我感到無聊:) – user1169629

2

使用此細胞存取權限,並修改它:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:your_row inSection:your_section]; 
    UITableViewCell *currentCell = [you_table cellForRowAtIndexPath:indexPath]; 
    id anySubview = [currentCell viewWithTag:subview_tag]; // Here you can access any subview of currentcell and can modify it. 

希望它可以幫助你。

0

這對我來說也有點混亂。因爲我不知道它是新創建的還是現有的。對於那種情況我使用

- (NSArray *)visibleCells 

UITableView的方法。並從數組中獲得。爲了確定哪一個是我想要使用的標記屬性或我添加indexpath屬性的單元格,這是在cellForRowAtIndexPath:方法中設置。如果單元格不在陣列中,則無論如何都不可見,並且將在用戶滾動時使用cellForRowAtIndexPath:方法創建。

相關問題