问题
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