我该如何使用
让 index = tableView.indexPathForSelectedRow? .行
collectionView TableView 中的代码?我想传递所选单元格的 indexPath 行数据。
到目前为止,我已经实现了 TableView 。
表格 View 源代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? MovieDetailViewController,
let index = tableView.indexPathForSelectedRow?.row
else {
return
}
destination.movieId = movieList?.movies[index].id
}
我需要的代码是 Collection View 版本的
让 index = tableView.indexPathForSelectedRow? .行
最佳答案
要获取 UICollectionView 中所选项目的索引路径,请使用
indexPathsForSelectedItems
The value of this property is an array of NSIndexPath objects, each of which corresponds to a single selected item. If there are no selected items, the value of this property is nil.
如果 allowsMultipleSelection
为 true
,indexPathsForSelectedItems
可以包含多个元素,否则只有一个元素。
所以,tableView
中的tableView.indexPathForSelectedRow
会对应
collectionView.indexPathsForSelectedItems?.first
关于ios - 如何在 Swift 的 collectionView 中传递 indexPath 行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57032591/