0

I want to implement multiple row selection in UITableview.but when i select multiple row with checkmark and scroll down,the checkmarks are disappears when i come back to selected rows section.I have tried many solutions from Stackoverflow but it doesn't work for me.Can you please anyone give me the solution which works for me.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *simpleTableIdentifier = @"SimpleTableCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
            if([_selectedstatearray containsObject:indexPath]) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else {
                cell.accessoryType = UITableViewCellAccessoryNone;

            }


        cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
        return cell;

    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellText = cell.textLabel.text;
        NSLog(@"cellText>>%@",cellText);

        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            [_selectedstatearray removeObject:cellText];
            cell.accessoryType = UITableViewCellAccessoryNone;

        } else {
            [_selectedstatearray addObject:cellText];
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }

        NSLog(@"selectedarray>>%@",_selectedstatearray);

        NSString *greeting = [_selectedstatearray componentsJoinedByString:@","];
        NSLog(@"%@",greeting);


        [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }
swapnil patel
  • 384
  • 2
  • 17

2 Answers2

2

The problem lies in this piece of code:

if([_selectedstatearray containsObject:indexPath]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

You are checking if _selectedstatearray contains the specified indexPath while you store them as NSString [_selectedstatearray addObject:cellText];

Replace the code above with this:

NSString *text = [statearray objectAtIndex:indexPath.row];
if([_selectedstatearray containsObject:text]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
The Mach System
  • 6,703
  • 3
  • 16
  • 20
  • Thanks alot.its working.and how to get this selected rows on other screen too? – swapnil patel Mar 07 '17 at 04:48
  • @swapnilpatel You have to maintain array. Whenever user select any row you get indexPath of that row so store `indexPath.row` in array and pass that array to that VC where you want selected rows. – dahiya_boy Mar 07 '17 at 04:52
  • If you want this tableView to notify the other view controller when a row gets selected, you can either create a custom protocol and set delegate to that view controller or you can use a block. If all you want is just to access the selected row from the other view controller, you can simply declare `_selectedstatearray` as a property in `.h` file. – The Mach System Mar 07 '17 at 04:54
  • selectedstatearray i already defined as property.then what?i have to pass these array to another viewcontroller? – swapnil patel Mar 07 '17 at 04:59
  • If it is already a property of view controller A, you can simply access it from another view controller (View Controller B) by calling `viewcontrollerA.selectedstatearray` – The Mach System Mar 07 '17 at 05:01
  • @ The Mach System....after passing array to another viewcontroller,how to display that array's contents in uitableview as a check marked rows? – swapnil patel Mar 07 '17 at 06:03
  • What do you mean? – The Mach System Mar 07 '17 at 22:15
0

Answer for your last comment

If you are passing selectedstatearray to another ViewController's (viewControllerB) UITableView which will contain only selectedstatearray then you can just add

cell.accessoryType = UITableViewCellAccessoryCheckmark

At your UITableVIew delegate method

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

Which will add check-mark to all row's

Hope it helps....

Mahbub Ahmed
  • 196
  • 1
  • 8