ios - 如何在 Swift 的 collectionView 中传递 indexPath 行数据

标签 ios swift uicollectionview

我该如何使用 让 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.

如果 allowsMultipleSelectiontrueindexPathsForSelectedItems 可以包含多个元素,否则只有一个元素

所以,tableView中的tableView.indexPathForSelectedRow会对应

collectionView.indexPathsForSelectedItems?.first

关于ios - 如何在 Swift 的 collectionView 中传递 indexPath 行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57032591/

相关文章:

ios - SwiftUI -> 线程 1 : Fatal error: No observable object of type MyObject. 找到类型(工作表中的 EnvironmentObject)

ios - 如何在 SwiftUI 中滚动时隐藏 SearchBar?

ios - IBM Worklight - IOS “Downloading Application update” 对话框在应用程序更新时卡住

ios - 使用 UIActivityViewController 检测启动了哪个 App

ios - 如何使用文本字段进行搜索?

ios - 动态 UICollectionView 单元格框架

ios - 如何在 swift 中使用 switch 显示 15 行?

objective-c - 自定义 iOS UILocalNotification 声音不播放(可能与 Xcode 更新有关)

java - 以地球为中心的地球经纬度高度在 Swift 中固定

快速组合 : How to create a single publisher from a list of publishers?