简体   繁体   中英

Can you downcast a NSIndexPath to NSInterger

I have a AVPlayer class that I'm using in a detail view that takes a indexPath of type Int.

I am getting the indexPath from the table view tableViewDidSelectAtIndexPath before sending it to the detail VC to use.

I tried to just simply downcast the NSIndexPath as! NSInteger before sending it to detail VC to use but it crashes without a crash message. So Im a bit stuck as to how to go about this.

Is it possible to downcast this? How can I get the number out of the NSIndexPath into an Int so I can use it in my class?

NSIndexPath is always* a combination of section and either row (for UITableView ) or item (for UICollectionView ).

you need to dissolve the section by accessing the single index:

id object = [[self dataSource] objectAtIndex:[indexPath row]];

* EDIT

Respective the comments: you are right. Multiple indexes are possible with NSIndexPath .

If you know you have multiple indexes (what I wouldn't expect in UITableViewDelegate ) you can iterate the indexes:

for (int i = 0; i < [indexPath length]; i++) {
    NSUInteger index = [indexPath indexAtPosition:i];
    // use index to access whatever you want
}

EDIT

PLEASE read documentation and headers before you ask the next time. Little hint: cmd - click NSIndexPath will take you to its header file with all possible informations.

Objective C

NSInteger currentRow = indexPath.row; 

Swift

let currentRow = self.tableView.indexPathForSelectedRow().row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM