在tableview中取消选择先前选择的单元格

时间:2012-06-13 11:36:55

标签: iphone uitableview

我有表视图,里面有多个部分。 我想只允许选择一行...我的tableview的选择属性设置为:SingleSelectioin

现在我这样做是为所选行设置复选标记

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

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

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

现在我想要的是,当我选择任何其他单元格时,先前选择的单元格应该设置为未选中。

我试过这个

lastIndexpath=indexPath;

然后

[tableView deselectRowAtIndexPath:lastIndexpath animated:YES]; 

但是它给了我对lasIndexPath的错误访问

EXC_BAD_ACCESS (code=1,address= 0xfe000008)

请帮助我

欢迎任何新建议

4 个答案:

答案 0 :(得分:3)

您想要标记您选择的单元格,然后在didSelectRowAtIndexPath中您可以编写如下代码:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

      cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedIndexPath];
        [cell setAccessoryType: UITableViewCellAccessoryNone];

     if(previousSelectedIndexPath!=nil)
    {
       [previousSelectedIndexPath release];
       previousSelectedIndexPath = nil;
    }
     previousSelectedIndexPath = [indexPath retain];
}

其中previousSelectedIndexPath是先前选择的索引;

答案 1 :(得分:2)

在控制器的.h中声明 NSIndexPath * lastIndexpath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
  if( lastIndexpath){
  if([tableView cellForRowAtIndexPath:lastIndexpath].accessoryType == UITableViewCellAccessoryCheckmark)

 {

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

 }}
  lastIndexpath=[indexPath retain]
 if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
 {
   [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
 }
 else
 {
  [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
 }
}

答案 2 :(得分:1)

//Take a variable of type indexpath in .h   

NSIndexPath    *myIndexPath

//Now check it in your cellForRowAtIndexPath: delegate method ..

if(indexPath == myIndexPath)
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
else 
     cell.accessoryType = UITableViewCellAccessoryNone;

//and in your didSelect: Delegate method ...

[tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark
myIndexPath = indexPath;
[tableView reloadData];

希望这会对你有所帮助。

答案 3 :(得分:1)

在委托方法willSelectRowAtIndexPath中,获取当前选中的索引路径,将accerroryType设置为none。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];

    [tableView cellForRowAtIndexPath:currentSelectedIndexPath].accessoryType = UITableViewCellAccessoryNone;

    if ([indexPath isEqualTo: currentSelectedIndexPath])
    {
        return nil;
    }
    return indexPath;
}