2011-11-24 64 views
4

我有一個約有20行的UITableView。我也使用複選標記附件來標識所選內容。我現在面臨的問題是,當我滾動時(這裏我選擇了多行),選擇會變得混亂。所以滾動後所選的checkmark已經消失。任何人都可以幫我找到一種方法嗎?在didSelectRowAtIndexPath:如何跟蹤UITableView中所有選定單元格的索引

NSMutableDictionary *rowDict = [tableList objectAtIndex:[indexPath row]]; 

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) 
{ 

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 

} 
else 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 

    checkedIndexPath = indexPath; 

    NSLog(@"the checked psths are :%@",checkedIndexPath); 

} 

而在cellforrowatIndexpath我用

if([self.checkedIndexPath isEqual:indexPath]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

但這僅適用於單一選擇。我應該怎麼做多選?

回答

2

您可以有一個NSMutableArray它可以添加n號碼NSIndexPath s對應的選定單元格。當單元格被取消選擇時,可以從數組中刪除該索引路徑。

+0

所以你說要怎麼做didSelectRowAtIndexPath方法,然後在的cellForRowAtIndexPath數組或取消選中創建單元正確匹配嗎? – rashii

+0

是的,你說得對 – Ilanchezhian

1

您應該使用數組來存儲檢查的索引。如果您使用單個屬性,則只有最後一個選定的行纔會獲得正確的accessoryType。

0

我用這個代碼很多次(按需要一些小的改動)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: 
       (NSIndexPath *)indexPath 
    { 

     UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; 


     if (thisCell.accessoryType == UITableViewCellAccessoryNone) 
     { 
      thisCell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     else 
     { 
      thisCell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } 

    - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView 
      accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
    { 
     //add your own code to set the cell accesory type. 
       return UITableViewCellAccessoryNone; 
     } 
相關問題