So I've written this code to put a checkmark beside a row that I want selected because I want multiple selected rows
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
but when I use the method:
NSArray *selectedIndexPaths = [self.LightsView indexPathsForSelectedRows];
it only gets the last row that I clicked on. Is the checkmark not selecting it?
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.
And you can use the tableview's editing property to distinguish how the cell should look like in your tableView:cellForRowAtIndexPath: method. So even turning off the move-functionality is easier by setting your tableview back to editing = NO , the move-icon would disappear and the accessoryType symbol shows up again.
For the indexPathsForSelectedRows:
method to work properly, you have to configure the table view to allow multiple selection of cells:
tableView.allowsMultipleSelection = YES;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With