简体   繁体   中英

Swift 3 UITableView Problems

My interface consists of two things inside my main ViewController: a label (the top half of the screen), and a table (the bottom half).

For the life of me, I cannot find a tutorial or example in Swift 3 + iOS 10 where a UITableView is successfully used, as opposed to a UITableViewController which takes up the entire screen.

What must be done to use a UITableView with dynamic cells in the bottom half of your interface?

I tried adding the UITableView, then creating a subclass of UITableViewController, but I was unable to select this in the UITableView's Custom Class -> Class dropdown. Apparently, I need to subclass UITableView, but I can find no examples of how to do this.

At first , the UITableView and UITableViewController are not the same thing, the TableView is a view and TableViewController is a Controller withe a tableview inside.

If you are using UITableViewController you should drag a UITableViewController, and then change its class in the third tab on the top!

If you are using a basic ViewController and drag a TableView inside, you could get The TableView in the ViewController using an IBOutlet.@John D.

You can use normal ViewController and init a UITableView in the viewDidLoad and add it to the ViewController.

var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView = UITableView.init(frame: CGRect(x:0, y: 300, height: 300, width:screenWidth))
    self.view.addSubview(tableView)
    self.tableView.register(UINib.init(nibName: "NameListTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: nameListTableViewCellId)
    // Do any additional setup after loading the view.
}

在此处输入图片说明

set the tableview.delegate = self and tableview.datasource = self , using methods in the pic and lots other in the UITableViewDelegate and UITabelViewDataSource protocol to Custom your tableView!

hope this would help

  • Use a standard UIViewController and drag a table view into the canvas.
  • In IB connect the table view to the IBOutlet and delegate and datasource to the controller.

     class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var tableView : UITableView! ... 

Unlike UITableViewController you have to implement all relevant datasource and delegate methods.

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