【问题标题】:Segue With UICollectionView使用 UICollectionView 进行转场
【发布时间】:2018-05-11 04:57:08
【问题描述】:

所以我有一个带有 CollectionView 和自定义 Cell 的 TableViewController,但我想准备从 CollectionView 到 TableView 的 segue。

我试过了:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "drawSegue" {
        let dst = segue.destination as! DrawerViewController

        guard let indexPath = cupCollectionView.indexPathsForSelectedItems else{
            return
        }
        dst.cup = cupboards[indexPath.row] as! Cupboard
    }
}

但我收到错误 Value of type '[IndexPath]' has no member 'row'。还有另一种方法吗?因为在另一个项目中,我将它从 tableView 使用到 tableView 并且它工作......

这是我的 CollectionView:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   return cupboards.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cupCell", for: indexPath) as! CupboardCollectionViewCell

    let cupboard = cupboards[indexPath.row]

    cell.cupLabel.text  = cupboard.name

    return cell
}

提前谢谢你

【问题讨论】:

    标签: ios swift uitableview core-data uicollectionview


    【解决方案1】:

    看起来 cupCollectionView.indexPathsForSelectedItems 正在返回一个 IndexPath 对象数组,因此您不是访问单个 IndexPath.row,而是尝试调用不存在的 Array<IndexPath>.row

    在你的didSelectCell调用performSegue时,将你的单元格添加为发件人,然后你可以将其更改为let indexPath = cupCollectionView.indexPath(for: sender)

    【讨论】:

    • 刚把它改成let indexPath = cupCollectionView.indexPath(for: sender as! UICollectionViewCell),看起来可以了,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2011-12-12
    • 2012-08-24
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多