我在嵌入UIButton
的{{1}}内部发现了UITableViewCell
(全部查看)。按下UICollectionView
时,它应将在UIButton
中找到的所有项目传递并列出到目的地UICollectionView
。
这是我的ViewController
的代码,其中包含UITableViewCell
和UIButton
:
UICollectionView
如何使 @IBOutlet weak var ViewAllButton: UIButton!
@IBOutlet weak var EventCollection: UICollectionView!
var events = [Events]()
override func awakeFromNib() {
super.awakeFromNib()
ViewAllButton.setIcon(prefixText: "View All ", prefixTextColor: .blue, icon: .typIcons(.chevronRight), iconColor: .blue, postfixText: " ", forState: .normal, iconSize: 24)
}
extension PopularCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return events.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = EventCollection.dequeueReusableCell(withReuseIdentifier: "EventCell", for: indexPath) as! EventCell
let event = events[indexPath.row]
print("Event Name:\(event.event_name)")
cell.event = event
cell.tag = indexPath.row
return cell
}
(全部查看)显示在UIButton
中找到的,指向目标UICollectionView
的特定索引处的项目?
答案 0 :(得分:0)
1. Add the following code with in a new file named UIView_Ext:
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
2. On View All button Action in the tableViewCell do the following:
self.parentViewController?.performSegue(withIdentifier: "Segue_Identifer", sender: self.events)
3. Implement prepare for segue in the view controller you are having table view controller reference as below:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Segue_Identifier" {
let destinationViewController = segue.destination as! DestinationViewController
destinationViewController.events = events
}
}