[英]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. 我在详细视图中使用的AVPlayer类采用了Int类型的indexPath。
I am getting the indexPath from the table view tableViewDidSelectAtIndexPath before sending it to the detail VC to use. 我从表视图tableViewDidSelectAtIndexPath获取indexPath,然后将其发送到详细信息VC以使用。
I tried to just simply downcast the NSIndexPath as! 我试图只是简单地将NSIndexPath转换为! NSInteger before sending it to detail VC to use but it crashes without a crash message.
NSInteger在将其发送给详细VC之前可以使用,但是它崩溃时没有崩溃消息。 So Im a bit stuck as to how to go about this.
因此,我对如何执行此操作有些困惑。
Is it possible to downcast this? 有可能对此cast之以鼻吗? How can I get the number out of the NSIndexPath into an Int so I can use it in my class?
如何将NSIndexPath中的数字获取到Int中以便可以在班级中使用它?
NSIndexPath
is always* a combination of section and either row (for UITableView
) or item (for UICollectionView
). NSIndexPath
始终*是节和行 (对于UITableView
)或项 (对于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
. NSIndexPath
可以使用多个索引。
If you know you have multiple indexes (what I wouldn't expect in UITableViewDelegate
) you can iterate the indexes: 如果您知道有多个索引(在
UITableViewDelegate
是我所不希望的),则可以迭代索引:
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. 小提示: cmd- 单击
NSIndexPath
将带您进入带有所有可能信息的头文件。
Objective C 目标C
NSInteger currentRow = indexPath.row;
Swift 迅速
let currentRow = self.tableView.indexPathForSelectedRow().row
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.