ios - 如何让 "View All"按钮通过 segue 到下一个 ViewController 显示数据?

标签 ios swift uitableview uicollectionview

我有一个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/

相关文章:

ios - 我想自己实现 promise ,但找不到解决方案

ios - 是否可以发出不打开应用程序的推送通知

swift - cocoa 图形故障

ios - UITableView deleteRows 未更新 IndexPaths 导致崩溃索引超出范围

iphone - uitableviewcell 的数据在滚动时相互重叠

导航 Controller 之前的 iOS 初始登录 View

ios - UIWebView 内存泄漏。释放未使用资源的魔法是什么?

swift - iBeacon 应该在 Swift 中需要蓝牙吗?

swift - Firebase Firestore "Could not build module ' nanopb'

ios - -[UITableView _configureCellForDisplay :forIndexPath:],/SourceCache/UIKit/UIKit-2935.137/UITableView.m:6509 断言失败