2011-11-24 135 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; 
     } 
相关问题