【问题标题】:UITableViewCell holding UICollectionView and click collection cell segueUITableViewCell 持有 UICollectionView 并单击收集单元格 segue
【发布时间】:2018-04-04 13:14:53
【问题描述】:

我正在使用 Swift3 和 Xcode 8.3.3
我正在研究包含 UICollectionView 的 UITableViewCell。 每个 CollectionView 单元都需要执行 segue。 我的意思是,我需要用导航控制器调用其他 ViewController。所以用户可以回到主 UITableViewController。

import UIKit

class PastCell: UITableViewCell {

    @IBOutlet weak var ptripDtls: UICollectionView!

    var logcount:Int = 0
    override func awakeFromNib() {
        super.awakeFromNib()

        ptripDtls.delegate = self
        ptripDtls.dataSource = self
        logcount = Transfer.tripObj[Transfer.cellpos]["logcount"]! as! Int

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let width = self.ptripDtls.collectionViewLayout.collectionViewContentSize.width;
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: width-17 , height: 50)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 1
        ptripDtls!.collectionViewLayout = layout
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

extension PastCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PastTripCollectionCell", for: indexPath) as! PastTripCollectionCell
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //Might be here segue function will call
    }

}

【问题讨论】:

  • 所以你想在选择集合视图单元格时调用另一个视图控制器
  • 你有什么问题?
  • 让 vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DestinationVC) as! DestinationVC self.show(vc, sender: self) 在你的 didselect 中写下这两行代码
  • @GoribDeveloper 这是包含集合的自定义单元格。所以我不能使用你的功能。
  • @V_rohit 是的,使用导航,所以我可以回来

标签: ios swift uitableview uicollectionview segue


【解决方案1】:

您实际上有多种选择:

关闭

当您将 tableView 单元格出列时,在 UITableViewCell 内传递一个闭包,如 tapped: (Model) -> Void,并在该闭包内指定执行转场。 (如果您的所有集合单元都执行相同的 segue,则它可以工作,如果应该调用不同的 segue,您可以使用此机制传播您的回调)

委托

定义一个自定义委托,通知您的 viewController 有关 CollectoinView 的 DidSelectItem 方法,然后相应地执行 Segue。

通知

在整个系统中触发一个通知,只有您的 ViewController 会监听该通知,并相应地执行您的 segue。 (这实际上不是一个好的解决方案,只是指出这是可能的)

【讨论】:

  • 我同意你的建议让我试试这个。
【解决方案2】:

对于 swift 4,如果您正在使用 segue,请在 didSelectRowAtIndexPath 中使用执行 segue

self.performSegue(withIdentifier: "YourSegueIdentifier", sender: nil)

如果您只使用导航控制器,请使用 Push。

let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourVCIdentifier") as! YourVC

self.navigationController?.pushViewController(vc, animated: true)

【讨论】:

  • 我已经写过我正在开发 swift3,并且 Table Custom Cell 类包含需要执行 segue 的集合和集合单元。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多