How to push VC from Custom CollectionViewCell Which is on TableViewCell?

為{幸葍}努か 提交于 2019-12-24 02:05:00

问题


I am Having a tableView and Cell, on Cell i have a collectionView and Displaying Some Content On it .

I want to send a link on selection of indexPath.

I want to push/present My View from Custom CollectionViewCell which is on TableViewCell.

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
    }
} 

回答1:


You just need to navigate using didSelectItemAtIndexPath same as Tableview control. Write your Navigation code to didSelectItemAtIndexPath of Collectionview




回答2:


If you are nesting a collection view inside a table view cell and want to trigger an action from the table view displaying this 1st cell, you can do two things.

First

You can use the UICollectionViewDelegate protocol to catch the user interaction with your collection view cell.

Don't forget to set your table view cell as the delegate of its collection view.

Second

You can create your own protocol (useful when a cell have multiple buttons).

Then, each button will have its own “didTap” method, instead of the “didSelect” method from the original collection view delegate.



来源:https://stackoverflow.com/questions/39849611/how-to-push-vc-from-custom-collectionviewcell-which-is-on-tableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!