【问题标题】:How to segue from UICollectionView Cell to another ViewController Swift如何从 UICollectionView Cell 到另一个 ViewController Swift
【发布时间】:2018-03-15 15:19:12
【问题描述】:

假设我有一个用户单元格。我需要为这个用户启动一个 ChatViewController。所以,我有 UICollectionView,它呈现为 raws(1 个单元格 = 1 行)。如何从单元格中转到 ChatViewController?

我根本不使用 InterfaceBuilder。只能通过代码方式。

我计划使用 didSelectItemAt,但我没有足够的快速编程技能来弄清楚如何将这个函数用于我的目的。当我尝试在 didSelectItemAt 函数中编写 prepare for segueperformSegue with Identifier 时,Xcode 没有提供正确的自动完成功能。我的代码如下:

import UIKit

class MyList: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    override init(frame: CGRect) {
        super .init(frame: frame)
        setUpLocalizedUserList()

    }


    //Click on User Cell
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Click")

    }


    //Localized User List (Collection View)
    private func setUpLocalizedUserList(){
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
        layout.minimumLineSpacing = 0
        layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 80)

        let userListFrame = CGRect(x: 0, y: 0, width: Int(UIScreen.main.bounds.width), height: Int(UIScreen.main.bounds.height) - 160)
        let myCollectionView:UICollectionView = UICollectionView(frame: userListFrame, collectionViewLayout: layout)

        myCollectionView.dataSource = self
        myCollectionView.delegate = self
        myCollectionView.register(LocalizedUserCell.self, forCellWithReuseIdentifier: "userCell")
        myCollectionView.register(GroupChatCell.self, forCellWithReuseIdentifier: "groupCell")
        myCollectionView.backgroundColor = UIColor.white

        addSubview(myCollectionView)
    }

    //Number of Section in User List
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }

    //Lists' Sizes
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (section == 0) ? 5 : 4
    }

    //Cells content here
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "userCell", for: indexPath) as! LocalizedUserCell
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupCell", for: indexPath) as! GroupChatCell
            return cell
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

也许这很重要 - 我的用户单元格位于另一个单元格中。

【问题讨论】:

  • 您只能从 UIViewController 而不是 UIView 执行Segue()。因此,您需要找到一种方法(关闭、委托、通知),让您的单元格告诉 UIViewController 显示它需要 performSegue()(可能带有一些参数)
  • 我正在尝试使用委托模式,但我不明白如何从可以使用这些方法的 UIViewController 传递委托变量。

标签: ios swift uicollectionview


【解决方案1】:

您不能从 inSide collectionViewCell 中进行转场,因为当前的 func 需要 UIViewController ,请参见此处我对表格单元格的回答,相同的逻辑 TableCellNavigate

【讨论】:

  • 谢谢!您的链接和答案是正确的。委托模式对我来说是正确的方式。
猜你喜欢
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
相关资源
最近更新 更多