Navjot Singh
Navjot Singh

Reputation: 65

How to uncheck checked rows in tableView

My code works exactly I want but Now I am unable to uncheck checked cells. Check the code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int z=0;
    if (indexPath.row==0)
    {
        NSArray *visibleCells = [tableView visibleCells];
        for (UITableViewCell *cell in visibleCells)
        {
            if (z==0)
            {
                [cell setAccessoryType:UITableViewCellAccessoryCheckmark];      
                 z++;
            }
            else
            {
                [cell setAccessoryType:UITableViewCellAccessoryNone];    
                z++;
            }
        }
    }
    else
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.checkedIndexPath = indexPath;
        [tableView reloadData];
    }
}

In this i have done coding to check multiple rows but if user select fist row all the other rows uncheck. this works perfect for me.

BUT NOW I WANT TO UNCHECK THE ROWS WHICH ARE CHECKED BEFORE. thanks in advance.

Upvotes: 1

Views: 626

Answers (2)

ShahiM
ShahiM

Reputation: 3268

If I understand correctly, you want to restore the previous selection. To do that, you would obviously have to store the selection somewhere. so create a class variable for that :

@implementation YourViewController
{
    NSArray *selectedIBackup;
}

Now, when selecting the first row make a backup of the current selection :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        selectedIBackup = _table.indexPathsForSelectedRows;

        for (int i=1; i<self.rowsCount; i++) {
            [_table deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
}

Now to restore the selection :

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (indexPath.row==0) {
        for (NSIndexPath *indexP in selectedIBackup) {
            [_table selectRowAtIndexPath:indexP animated:NO];
        }
   }
}

Upvotes: 1

Rushang Prajapati
Rushang Prajapati

Reputation: 596

Check My code you can easily add the multiple value in MutuableArray by in click if you uncheked then it will remove by indexpath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSMutableArray *index2=[[NSMutableArray alloc]init];
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

       if (indexPath.section == 0){


          if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {       
               [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
               [self.click insertObject:[service_shop objectAtIndex:indexPath.row]atIndex:0];

               NSLog(@"-%@--%@--",click);

                [index2 addObject:indexPath];  
          }else{

               [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
               [click removeObject:[service_shop objectAtIndex:indexPath.row] ];

                NSLog(@"--%@--%@---",click);
                [index2 removeObject:indexPath];
            }
            [_tableView reloadData];
        }
 }

Hope this work

Thanks

Upvotes: 2

Related Questions