【发布时间】:2016-12-12 08:14:45
【问题描述】:
如何在 CollectionView 中使用 indexPathForSelectedRow?
CollectionView/TableView:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("levelCell", forIndexPath: indexPath) as! CollectionViewCell
guard levelListen != nil else {
return cell
}
let levelListe = levelListen[indexPath.row]
cell.levelLabel?.text = levelListe.name
return cell
}
TableView - prepareForSegue(工作):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "zumLevel" {
let dstCtl = segue.destinationViewController as! InLevelViewController
let indexPath = tableView.indexPathForSelectedRow!
dstCtl.levelList = levelListen[indexPath.row]
}
}
CollectionView - prepareForSegue(不起作用):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "zumLevel" {
let dstCtl = segue.destinationViewController as! InLevelViewController
// like TableView?
let indexPath = collectionView.indexPathsForSelectedItems()!
dstCtl.levelList = levelListen[indexPath.row] // <--- .row doesn't work, should I use .count ?
}
}
当我尝试 .count 时,它说:“在展开可选值时意外发现 nil”
【问题讨论】:
-
嗯。您可以在 collectionView 和 tableView 上尝试 didselectrowatindexpath 吗?并根据选择更新一个变量,然后调用您的 performSegueWithIdentifier 并将新更新的变量传递给新的 ViewController。
-
KotaBear233 似乎明白你想要什么,但我不明白。 为什么你想在
CollectionView中使用indexPathForSelectedRow?你想达到什么目的?第二个sn-p实现了什么,为什么还不够?
标签: ios swift uitableview core-data uicollectionview