ios - 无法从具有 indexPath 的单元格执行 segue
问题描述
我正在尝试执行 segue 并将数据从我的单元格中传递给 detailViewController。我已经尝试过使用虚拟数据,我可以将它们传递给 detailScreen,但是我想传递与单元格相关的数据。当我点击一个单元格时,我收到此错误“无法将 ViewController 类型的值转换为 MovieCell”,其中一行是 let selectedItem = ...感谢您的时间和精力这是我的代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue"
{
let destination = segue.destination as! DetailViewController
let selectedItem = collectionView.indexPath(for: sender as! MovieCell)!
let character = characters[selectedItem.item]
destination.detailName = character.name
destination.detailGender = character.gender
destination.detailSpecies = character.species
destination.detailStatus = character.status
}
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "detailSegue", sender: self)
}
解决方案
尝试使用:
collectionView.indexPathsForSelectedItems.first
反而
collectionView.indexPath(for: sender as! MovieCell)!
另外,我想为您推荐我的图书馆以应对这种情况:链接
推荐阅读
- python - 如何在不安装的情况下在应用程序之间共享 python 类?
- css - Rails 5 - Heroku:预编译资产失败(Sass::SyntaxError: Invalid CSS after "...lflings-regular": expected ")
- notifications - 拨打大量电话后,RingCentral 停止在沙盒中发送呼叫通知
- safari - 如何在 Safari 中获取视频的播放头?
- python - Python 的 unittest 库如何匹配通过 -p 参数传递的模式?
- reactjs - Firebase.Firestore 不是函数
- python - Get a particluar edge attribute of a networkX graph
- python - BeautifulSoup 从