【问题标题】:How to pass indexPath row data in collectionView in Swift如何在 Swift 中的 collectionView 中传递 indexPath 行数据
【发布时间】:2019-07-15 13:13:55
【问题描述】:

如何使用 let index = tableView.indexPathForSelectedRow? .Row collectionView的表格视图中的代码?我想传递所选单元格的 indexPath 行数据。

到目前为止,我已经实现了表格视图。

tableView源码

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

    }

我需要的代码是一个集合视图版本 let index = tableView.indexPathForSelectedRow? .Row

【问题讨论】:

  • 尝试使用indexPathsForSelectedItems
  • @koropok destination.movieId = movieList? .movies [index] .id此代码未实现。
  • 很抱歉,但我认为您必须提供更多信息才能为您提供帮助。

标签: ios swift uicollectionview


【解决方案1】:

要获取 UICollectionView 中 selectedItems 的 indexPaths,请使用

indexPathsForSelectedItems

这个属性的值是一个 NSIndexPath 对象的数组,每个对象 它对应于单个选定的项目。如果没有选择 items,该属性的值为 nil。

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

所以,tableView 中的tableView.indexPathForSelectedRow 将对应于

collectionView.indexPathsForSelectedItems?.first

【讨论】:

    猜你喜欢
    • 2020-11-03
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多