Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you get the selected rows from a UITableView?

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?

like image 396
Pittfall Avatar asked Apr 08 '13 18:04

Pittfall


People also ask

How do you get the indexPath row when a button in a cell is tapped?

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.

What does indexPath row return?

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.

Which method is used to allow moving of row in UITableView?

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.


1 Answers

For the indexPathsForSelectedRows: method to work properly, you have to configure the table view to allow multiple selection of cells:

tableView.allowsMultipleSelection = YES;
like image 190
Daniel Martín Avatar answered Sep 26 '22 15:09

Daniel Martín