2012-06-13 137 views
1

我有其中包含多个部分的表格视图。 我想选择的只有一排...我的tableview的选择属性被设置为允许:SingleSelectioin在tableview中取消选择先前选择的单元格

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

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) 

坏接入请帮助我这个

任何新建议也欢迎

+0

您是否在使用ARC?如果不尝试:[lastIndexpath release]; lastIndexpath = [indexPath retain]; – iTukker

+0

是的现在它不是给我BAD_ACCESS,但我想要的是,一次只应选择一行 – Anand

+0

你能检查以下吗? lastIndexpath = indexPath.row; –

回答

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以前选择指数;

+0

什么是previousSelecedIndexPath以及如何为其分配值 – Anand

+0

它与您的lastIndexpath相同,即之前选择的indexPath。 –

2

声明NSIndexPath * lastIndexpath在.H控制器的:

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

希望这会帮助你。

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

谢谢大家,我的问题得到解决只是通过保留和使用lastIndexpath ... – Anand

相关问题