我有一个UIButton
(查看全部)在 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
中找到的项目在目的地的特定索引 ViewController
?
最佳答案
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
}
}
关于ios - 如何让 "View All"按钮通过 segue 到下一个 ViewController 显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57052398/