Swift - How to get numeric values ​​for selected rows in UITableview

I have multiple selections in UITableview made in Swift, and I declare an array containing NSIndexPaths selected UITableview Cells.

 self.selectedRows = self.preferencesTableView.indexPathsForSelectedRows() 

How to convert this array to readable terms. For example, self.selectedRows NSlogged:

Selected items Strings [{length = 2, path = 0 - 1}, {length = 2, path = 0 - 0}, {length = 2, path = 0 - 3}]

I would like to convert this to: 1,2,3.

In Objective-C, I enumerate ObjectsWithOptions through an array and add the id of the array to the mutable array to get what I want.

 [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 to do it in Swift?

+6
source share
2 answers

You can use the map function to return an array of indices. It takes a closure as an argument and iterates over each element of the array. $0 is the first argument, in our case this is the selected pointer path:

 let rows = self.tableView.indexPathsForSelectedRows()?.map{$0.row} 

Note that the rows constant will be optional

+11
source

for swift 2.3

 let rows = tableView.indexPathsForSelectedRows.map{$0.map{$0.row}} 
+1
source

Source: https://habr.com/ru/post/986486/


All Articles