user3349728
user3349728

Reputation: 11

How to select multiple cells in a table view

I have a table view with multiple cells. When I click on one of the cell it navigates to the next view controller, but my problem is that I cannot select multiple cells. Could any one please help me on this issue, I want to select two or more cells with two finger tap.

Upvotes: 1

Views: 808

Answers (4)

Irfan
Irfan

Reputation: 4331

Since cells are being reused, you need to set the accessory mark to on or off for every cell in the table in the cellForRowAtIndexPath table datasource method.

So the cell.accessoryType cell property should be specified in the cellForRowAtIndexPath and not the didSelectRowAtIndexPath delegate method.

In the didSelectRow, just keep track of the selected rows in an array, and set the cells accessory mark to none or checkmark in the cellForRowAtIndexPath depending on the array value.

Upvotes: 0

iLearner
iLearner

Reputation: 1680

If you are implementing the tableview programatically then the better way is to create a custom cell which will have button (and obviously other UI components that you need to show in the table view cell) which will work as a checkbox and assign button.tag as indexPath.row which will help you to select multiple rows.

Take a look here -- Below code will go into cellForRowAtIndexPath

YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:@selector(multipleCheckAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;


-(void)multipleCheckAction:(UIButton *)sender { //sender.tag will be equal to indexPath.row }

Happy Coding

Upvotes: 0

mohitdream4u
mohitdream4u

Reputation: 166

set by code if you set by programming

table.allowsMultipleSelection = YES;

and if you set in xib tick on allowsMultipleSelection

Upvotes: 1

sabeer
sabeer

Reputation: 603

Just add button like checkbox and multiple choose like checkbox.

Upvotes: 0

Related Questions