OGeek|极客世界-中国程序员成长平台

标题: ios - 如何从 TableViewCell 上的自定义 CollectionViewCell 推送 VC? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:43
标题: ios - 如何从 TableViewCell 上的自定义 CollectionViewCell 推送 VC?

我有一个 tableView 和 Cell,在 Cell 上我有一个 collectionView 并在上面显示一些内容。

我想发送选择 indexPath 的链接。

我想从 TableViewCell 上的自定义 CollectionViewCell 推送/呈现我的 View 。

class secondTopicTableViewCell: UITableViewCell {
    @IBOutlet weak var relatedCustom: UICollectionView!
    var relArray  = NSArray()
     func loadArray(arr: NSArray) {
        self.relArray = arr
        self.relatedCustom.reloadData()
    }
}

extension secondTopicTableViewCell : UICollectionViewDataSource {

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collection", forIndexPath: indexPath) as! relatedCollectionViewCell
        let info = self.relArray.objectAtIndex(indexPath.row) as! specificTopicInfo
        cell.showInfo(info)
        return cell
    }
}



extension secondTopicTableViewCell : UICollectionViewDelegate {

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let relatedTopic =  self.relArray.objectAtIndex(indexPath.row)  as! specificTopicInfo

        let str  = relatedTopic.relatedLink!
        print(str)
    }
}



class relatedCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var relatedLabel: UILabel!

    func showInfo(info: specificTopicInfo) {
        relatedLabel.backgroundColor = UIColor.grayColor()
        relatedLabel.text = info.relatedTitle
    }
} 



Best Answer-推荐答案


您只需要使用与 Tableview 控件相同的 didSelectItemAtIndexPath 进行导航。 将导航代码写入 Collectionview 的 didSelectItemAtIndexPath

关于ios - 如何从 TableViewCell 上的自定义 CollectionViewCell 推送 VC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849611/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4