[英]Swift 2.0 error Downcast from '[NSIndexPath]?' to '[indexpath]' only unwraps optional
[英]Can you downcast a NSIndexPath to NSInterger
我在詳細視圖中使用的AVPlayer類采用了Int類型的indexPath。
我從表視圖tableViewDidSelectAtIndexPath獲取indexPath,然后將其發送到詳細信息VC以使用。
我試圖只是簡單地將NSIndexPath轉換為! NSInteger在將其發送給詳細VC之前可以使用,但是它崩潰時沒有崩潰消息。 因此,我對如何執行此操作有些困惑。
有可能對此cast之以鼻嗎? 如何將NSIndexPath中的數字獲取到Int中以便可以在班級中使用它?
NSIndexPath
始終*是節和行 (對於UITableView
)或項 (對於UICollectionView
)的組合。
您需要通過訪問單個索引來分解該部分:
id object = [[self dataSource] objectAtIndex:[indexPath row]];
*編輯
分別評論:您是對的。 NSIndexPath
可以使用多個索引。
如果您知道有多個索引(在UITableViewDelegate
是我所不希望的),則可以迭代索引:
for (int i = 0; i < [indexPath length]; i++) {
NSUInteger index = [indexPath indexAtPosition:i];
// use index to access whatever you want
}
編輯
在下次詢問之前, 請閱讀文檔和標題。 小提示: cmd- 單擊 NSIndexPath
將帶您進入帶有所有可能信息的頭文件。
目標C
NSInteger currentRow = indexPath.row;
迅速
let currentRow = self.tableView.indexPathForSelectedRow().row
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.