[英]Swift - How to get number values for selected rows in UITableview
I have a multiple selection on a UITableview
made in Swift and I declare an Array that holds the NSIndexPaths
of the selected UITableView
Cells. 我在Swift中创建的
UITableview
上有多个选择,我声明了一个包含所选UITableView
Cells的NSIndexPaths
的Array。
self.selectedRows = self.preferencesTableView.indexPathsForSelectedRows()
How do I convert this array to readable terms. 如何将此数组转换为可读术语。 eg self.selectedRows is NSlogged like :
例如self.selectedRows是NSloglike:
Selected Items Strings [ {length = 2, path = 0 - 1}, {length = 2, path = 0 - 0}, {length = 2, path = 0 - 3}] 选定项目字符串[{length = 2,path = 0 - 1},{length = 2,path = 0 - 0},{length = 2,path = 0 - 3}]
I wan to be able to convert this to : 1,2,3 . 我希望能够将其转换为:1,2,3。
In Objective CI enumerateObjectsWithOptions through the array and add the id of the array to a mutable array to get what I want. 在Objective CI中,通过数组enumerateObjectsWithOptions并将数组的id添加到可变数组中以获得我想要的内容。
[self.selectedRows enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = obj;
[self.selectedCategorieItems addObject:[[[self.categoryArr
objectAtIndex:0] objectForKey:@"id"]objectAtIndex:indexPath.row]];
}];
How do I do this in Swift? 我如何在Swift中执行此操作?
You can use map
function to return array of indices. 您可以使用
map
函数返回索引数组。 It takes closure as argument and iterate over each element of array. 它将闭包作为参数并迭代数组的每个元素。
$0
is first argument, in our case this is selected index path: $0
是第一个参数,在我们的例子中这是选择的索引路径:
let rows = self.tableView.indexPathsForSelectedRows()?.map{$0.row}
Please note that rows
constant will be optional 请注意,
rows
常量是可选的
对于swift 2.3
let rows = tableView.indexPathsForSelectedRows.map{$0.map{$0.row}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.