1

我有包含多个部分的表格视图。我只想选择一行...我的 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

4 回答 4

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 是先前选择的索引;

于 2012-06-13T11:49:10.677 回答
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;
 }
}
于 2012-06-13T11:57:57.420 回答
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];

希望这会帮助你。

于 2012-06-13T12:27:32.590 回答
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;
}
于 2012-06-13T12:41:01.843 回答