简体   繁体   English

如何在 UIView 中添加 UITableviewController

[英]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.我有一个视图 controller ,它是UITableViewcontrollersubclass ,我需要在一个UIView下显示UITableViewcontroller ,我需要分配Corner Radius以便它与我的设计相匹配。

UITableViewcontroller is the Generic tableview class which I have used in the whole project so I couldn't make changes in the structure. UITableViewcontroller 是我在整个项目中使用的通用表格视图tableview ,因此我无法更改结构。

All my ViewController are created programmatically, i have not used anywhere Storyboard.我所有的 ViewController 都是以编程方式创建的,我没有在任何地方使用过 Storyboard。

Here is my Viewconroller Code where I am implementing这是我正在实施的 Viewconroller 代码

  1. headerViewController is part which i mark as white headerViewController 是我标记为白色的部分
  2. deal and team controller is my UITableviewController which i have added in SJSegment交易和团队 controller 是我在 SJSegment 中添加的 UITableviewController
     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.据我了解,您想在其他视图 controller 中添加视图 controller 作为子视图。 You have to use ContainerView in the View Controller where you want to show that table Table View Controller.您必须在视图 Controller 中使用 ContainerView,您要在其中显示该表 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) .您需要UITableViewview.addSubview(yourCustomClass) It is not necessary to add UITableViewController inside your UIView没有必要在 UIView 中添加UITableViewController

Controllers it's also a basic class that can control your view.控制器它也是一个基本的 class 可以控制您的视图。 You can add as subview tableView you your generic view and set their delegate and dataSource to the main view controller.您可以将您的通用视图添加为子视图 tableView,并将它们的委托和数据源设置为主视图 controller。 You don't need to create tablewviewcontroller for this.您不需要为此创建 tablewviewcontroller。

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在此之后,您可以自定义您的表格视图,如视图并做您想做的事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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