简体   繁体   English

在表视图中取消选择先前选择的单元格

[英]deselect the previously selected cell in tableview

I have Table View with Multiple sections in it. 我有带有多个部分的表视图。 I want to allow only one row selected...My tableview's Selection Property is Set to:SingleSelectioin 我只允许选择一行...我的表视图的选择属性设置为:SingleSelectioin

Now I am doing this to set the checkmark for selected row 现在我正在执行此操作以设置选中行的选中标记

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

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

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

Now what I want is that when I select any other cell, the previously selected cell should be set back to unselected. 现在我想要的是,当我选择任何其他单元格时,先前选择的单元格应设置回未选择状态。

I have tried with this 我已经尝试过了

lastIndexpath=indexPath;

and then 接着

[tableView deselectRowAtIndexPath:lastIndexpath animated:YES]; 

but it gives me bad access on lasIndexPath 但这给我在lasIndexPath上的错误访问

EXC_BAD_ACCESS (code=1,address= 0xfe000008)

Kindly Help me with this 请帮助我

any New Suggestions are also welcomed 任何新建议也欢迎

You want to mark your selected cell, then in didSelectRowAtIndexPath you can write code like this: 您要标记选定的单元格,然后在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];
}

where previousSelectedIndexPath is previously selected index; 其中previousSelectedIndexPath是先前选择的索引;

Declare NSIndexPath *lastIndexpath in .h of your controller: 在控制器的.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;
 }
}
//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];

Hope this will help you. 希望这会帮助你。

in delegate method willSelectRowAtIndexPath , get the current selected index path, set accerroryType to none. 在委托方法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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM