从分离的UITableView推送/呈现ViewController

时间:2017-10-22 14:51:50

标签: ios swift uiviewcontroller

我有一个ticketVC,它有一个segmentedControl,用于切换两个tableViews(即即将到来的ViewView和pastTransactionsTableView)。这两个tableView是在不同的文件上创建的。我想从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
    }
}

我采用这种方法的原因是,comingTableView和pastTransactionsTableView都显示了非常不同的单元格,并提取了单独的模型。有没有人有任何建议我如何从这些tableViews本身推送/呈现一个新的VC?

1 个答案:

答案 0 :(得分:0)

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

enter image description here

我更喜欢将UITableView保留在UIViewController中,因为我总是在里面有其他内容。 现在创建两个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])
    }
}

现在返回ticketVC并声明变量

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更新呈现的视图控制器

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

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