ios 如何从 UITableView 获取选定的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15886493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you get the selected rows from a UITableView?
提问by Pittfall
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?
它只获取我点击的最后一行。复选标记没有选中它吗?
回答by Daniel Martín
For the indexPathsForSelectedRows:
method to work properly, you have to configure the table view to allow multiple selection of cells:
为了使该indexPathsForSelectedRows:
方法正常工作,您必须配置表格视图以允许多选单元格:
tableView.allowsMultipleSelection = YES;