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

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

EXC_BAD_ACCESS (code=1,address= 0xfe000008)

Kindly Help me with this

any New Suggestions are also welcomed

Answers


You want to mark your selected cell, then in didSelectRowAtIndexPath you can write code like this:

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


Declare NSIndexPath *lastIndexpath in .h of your controller:

- (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.

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

    [tableView cellForRowAtIndexPath:currentSelectedIndexPath].accessoryType = UITableViewCellAccessoryNone;

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

Need Your Help

iOS app crashes on in-app purchase, only on App Store version

ios iphone objective-c app-store in-app-purchase

It's my first time doing in-app purchases. Everything seemed to work fine when I tested on my device (iPod 4 Touch), but after submitting the app to the App Store, I downloaded it and the app crash...

jQuery - drag-and-drop visual feedback on draggable clone

jquery drag-and-drop clone draggable droppable

Using droppable to provide feedback is not a good option due to their deeply nested structure (using greedy), so I would like to give visual feedback by changing the draggable (i.e., the clone).