iosobjective-cnsarraynsindexpathnsindexset

How can I select elements from an NSArray according to array of NSIndexPaths


Is there a fast way to get all elements at indexes from an array returned from a UITableView (NSArray of NSIndexPaths).

For instance:

[self.dataSourceArray selectItemsAt:[self.tableView indexPathsForSelectedItems]]

Solution

  • There is no built-in method, but you can simply loop over the selected rows (assuming that there is only one section) and add the corresponding elements to a mutable array:

    NSMutableArray *selectedObjects = [NSMutableArray array];
    for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) {
        [selectedObjects addObject:self.dataSourceArray[indexPath.row]];
    }