简体   繁体   中英

How to add UITableviewController inside UIView

I have one view controller which is a subclass of UITableViewcontroller , that UITableViewcontroller i need show under one UIView which I need to assign Corner Radius so it will match with my design.

UITableViewcontroller is the Generic tableview class which I have used in the whole project so I couldn't make changes in the structure.

All my ViewController are created programmatically, i have not used anywhere Storyboard.

Here is my Viewconroller Code where I am implementing

  1. headerViewController is part which i mark as white
  2. deal and team controller is my UITableviewController which i have added in SJSegment
     private func setupSegmentController() {
        segmentViewController.headerViewHeight = 350
        segmentViewController.headerViewController = headerViewController

        segmentViewController.segmentControllers = [
            dealViewController,
            teamViewController]
        segmentViewController.delegate = self
        
        addChild(segmentViewController)
        segmentViewController.view.frame = view.bounds
        view.addSubview(segmentViewController.view)
        segmentViewController.didMove(toParent: self)
        
      }

Below in red highlighted is the area which i need to design

在此处输入图像描述

What I understand you want to add a view controller as child in some other view controller. You have to use ContainerView in the View Controller where you want to show that table Table View Controller. Create an outlet of that Container View then add the table view as child.

let yourTableViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourTableViewController") as! UITableViewcontroller
yourTableViewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
yourTableViewController.view.frame = self.containerView.bounds
self.addChild(yourTableViewController)
containerView.addSubview(yourTableViewController.view)
yourTableViewController.didMove(toParent: self)

You need to subclass UITableView and view.addSubview(yourCustomClass) . It is not necessary to add UITableViewController inside your UIView

Controllers it's also a basic class that can control your view. You can add as subview tableView you your generic view and set their delegate and dataSource to the main view controller. You don't need to create tablewviewcontroller for this.

all you need is - (inside main view controller)

tableView.delegate = self
tableView.dataSource = self.

and after this you must implement protocol conformance

MainViewController: UITableViewCOntrollerDelegate {

// implementation here
}
MainViewController: UITableViewControllerDataSource {
// implementation here
}

After this you can customise your table view like view and do what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM