ios - 从分离的 UITableView 推送/呈现 ViewController

标签 ios swift uiviewcontroller

我有一个 ticketsVC,它有一个 segmentedControl,可以切换两个 tableView(一个即将到来的 TableView 和一个 pastTransactionsTableView)。这两个 tableViews 是在不同的文件上创建的。我想从 tableViews 推送/呈现一个新的 VC,但不幸的是我无法从这些 tableViews 访问 navigationController 或 presentVC。

我的做法是这样的:

class TicketsViewController: UIViewController {

   let segmentedControllerView: SegmentedController = {
        let sc = SegmentedController()
        sc.translatesAutoresizingMaskIntoConstraints = false
        sc.segmentedController.addTarget(self, action: #selector(segmentedControlValueChanged), for: .valueChanged)
        sc.segmentedController.selectedSegmentIndex = 0
        return sc
    }()

    let upcomingTableView: UpcomingTableView = {
        let tv = UpcomingTableView(frame: .zero, style: .grouped)
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
    }()

    let pastTransactionsTableView: PastTransactionsTableView = {
        let tv = PastTransactionsTableView(frame: .zero, style: .grouped)
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        //setupNavigationBar()
        //setupViews()
    }

    @objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
        let segmentedControl = sender

        if segmentedControl.selectedSegmentIndex == 0 {
            upcomingTableView.isHidden = false
            pastTransactionsTableView.isHidden = true
        } else {
            upcomingTableView.isHidden = true
            pastTransactionsTableView.isHidden = false
        }
    }

//Both UpcomingTableView and PastTransactionsTableView
//are created similar as below
class UpcomingTableView: UITableView {

    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)

        delegate = self
        dataSource = self

        register(CustomCell.self, forCellReuseIdentifier: "cell")

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension UpcomingTableView: UITableViewDataSource, UITableViewDelegate {
    //all the neccessary dataSource and delegate functions

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = TicketsViewController()
        vc.present(UIViewController(), animated: true, completion: nil)
        //This method cannot access TicketsVC
    }
}

我采用这种方法的原因是即将到来的 TableView 和 pastTransactionsTableView 显示非常不同的单元格并拉出单独的模型。有没有人对我如何从这些 tableViews 本身推送/呈现新的 VC 有任何建议?

最佳答案

如果我理解正确的话,我会这样做: 在 ticketsVC 中创建一个分段控件并将其定位到顶部。然后在下面添加一个简单的 UIView,我们将用作表格的容器。它在 Storyboard 中应该是这样的:

enter image description here

我更喜欢将 UITableView 保留在 UIViewController 中,因为我总是在里面有其他东西。 现在创建两个 UIViewControllers 并在每个 UIViewControllers 中添加一个 UITableView 并在这两个 UIViewControllers 中做所有你需要做的与表相关的事情。

    class SampleOneVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Your code for table 1
}

    class SampleTwoVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Your code for table 2
}

为 UIViewController 创建一个扩展:

extension UIViewController {
    func configureChildViewController(childController: UIViewController, onView: UIView?) {
        var holderView = self.view
        if let onView = onView {
            holderView = onView
        }
        addChildViewController(childController)
        holderView!.addSubview(childController.view)
        constrainViewEqual(holderView: holderView!, view: childController.view)
        childController.didMove(toParentViewController: self)
    }


    func constrainViewEqual(holderView: UIView, view: UIView) {
        view.translatesAutoresizingMaskIntoConstraints = false

        let pinTop = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: holderView, attribute: .top, multiplier: 1.0, constant: 0)
        let pinBottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: holderView, attribute: .bottom, multiplier: 1.0, constant: 0)
        let pinLeft = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: holderView, attribute: .left, multiplier: 1.0, constant: 0)
        let pinRight = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: holderView, attribute: .right, multiplier: 1.0, constant: 0)

        holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
    }
}

现在回到ticketsVC并声明变量

var sampleOneVC: UIViewController!
var sampleTwoVC: UIViewController!

在 ViewDidLoad 中初始化它们

// Don't forget to set the IDs in storyboard (Identity Inspector tab) for view controllers 
// field: 'Storyboard ID' 
// values: SampleOneVC & SampleTwoVC
sampleOneVC = mainStoryboard.instantiateViewController(withIdentifier: "SampleOneVC") as! SampleOneVC
sampleTwoVC = mainStoryboard.instantiateViewController(withIdentifier: "SampleTwoVC") as! SampleTwoVC

现在在 segmentedControlValueChanged 中更新呈现的 View Controller

self.configureChildViewController(childController: sampleOneVC, onView: myContainerView)

现在您应该能够在 SampleOneVC 和 SampleTwoVC 的 didSelectRowAt 中使用 segues。我没有写完整的代码,但我希望你知道如何用另一种方式来完成它。

关于ios - 从分离的 UITableView 推送/呈现 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46875459/

相关文章:

ios - 简单的 UIPickerView 错误

ios - 在 Swift 中以编程方式在 UITableViewCell 中创建 UICollectionView

ios - UITableviewCell accessoryView 无法更改

ios - 当 SwiftUI Widget 的相对文本字段计数器为零时停止?

swift - 跟踪 NSCell 的位置变化

ios - 如何从父 View Controller 检测容器 View 中的焦点 UITextField

iphone - 如何获取联系人的显示名称属性

ios - UIDocumentInteractionController 更改呈现 View 的标题

ios - viewDidDisappear 后键盘不会消失

ipad - UIViewControllerHierarchyInconsistency 在 iPad 方向 iOS 6.1.3 期间发生