2014-01-22 41 views
2

我在更新UITableView中的行時遇到問題。在我的tableView中,只能選擇一行中的一行(可能有幾個部分)。所以,當用戶選擇行時,我以編程方式取消選擇本節中的其他行。當取消選中的行可見時,一切正常。如果任何取消選擇的行不可見,那麼它仍然被檢查。但didDeselectRowAtIndexPath被稱爲此行。爲什麼?如何解決這個問題?取消選擇不可見行

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    // It is predefined group - only one row in section cab be selected 
    // Deselect other rows in section 
    for (NSIndexPath * selectedIndexPath in tagTableView.indexPathsForSelectedRows) { 
     if (selectedIndexPath.section == indexPath.section) { 
      [self tableView:tableView didDeselectRowAtIndexPath:selectedIndexPath]; 
      [tableView deselectRowAtIndexPath:selectedIndexPath animated:YES]; 
     } 
    } 

    return indexPath; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tagTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

回答

1

選擇的細胞的狀態不應該保存在UITableViewCell,但在對象(模型),填補細胞。

由於表格單元被重用,所以狀態代表填充模型的最後一個狀態。你應該保持選中的狀態在填充單元格的對象中,並保持一個選擇索引路徑的數組。

-tableView:cellForRowAtIndexPath:中設置該單元的正確狀態。如果模型對象保持選定的狀態,則可以根據該屬性設置狀態,或者如果使用選擇的索引路徑保留數組,請檢查路徑是否在數組中並相應地設置狀態。

+0

如果選中的行不可見,則無法取消選擇,UIKit中存在一個錯誤。然後它卡住了。 – Andy