如何在UITableView中跟踪所有选定单元格的索引

时间:2011-11-24 04:54:11

标签: iphone

我有UITableView,大约有20行。我还使用复选标记附件来识别所选的。我现在面临的问题是当我滚动时(这里我选择多行),选择变得混乱。所以在滚动后,所选的checkmark已经消失了。任何人都可以帮我找到方法吗?在didSelectRowAtIndexPath:

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;
}

但这仅适用于单一选择。我应该怎样做多选?

3 个答案:

答案 0 :(得分:2)

您可以拥有一个NSMutableArray,可以添加n个与所选单元格对应的NSIndexPath个。取消选择单元格后,可以从数组中删除该索引路径。

答案 1 :(得分:1)

您应该使用数组来存储已检查的索引。如果您使用单个属性,则只有最后选择的行才能获得正确的accessoryType。

答案 2 :(得分: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;
      }