如何从UITableView获取所选行?

时间:2022-08-25 23:23:39

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?

它只获取我点击的最后一行。复选标记是不是选择它?

1 个解决方案

#1


22  

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

要使indexPathsForSelectedRows:方法正常工作,您必须配置表视图以允许多个单元格选择:

tableView.allowsMultipleSelection = YES;

#1


22  

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

要使indexPathsForSelectedRows:方法正常工作,您必须配置表视图以允许多个单元格选择:

tableView.allowsMultipleSelection = YES;