如何将编程约束添加到UITableView? [英] How to add programmatic constraints to a UITableView?

查看:58
本文介绍了如何将编程约束添加到UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级设置如下:

class SettingsController: UITableViewController {

我希望能够向UITableView添加约束,以便表的两边都具有相等的间距(当前是全宽表,因此在iPad上有点难看).

I would like to be able to add constraints to the UITableView so that there is equal spacing either side of the table (it is currently a full width table, so a little ugly on iPad).

我已阅读到我无法将约束添加到 UITableViewController ,而是必须将 UIViewController 添加到类中,添加 UITableView ,然后我应该能够将约束添加到TableView.

I have read that I can't add constraints to a UITableViewController, and instead I must add a UIViewController to my class, add a UITableView to that, and then I should be able to add the constraints to the TableView.

我已经修改了该类,并在下一行定义了 UITableView .所以这是我的前两行:

I've amended the class and defined the UITableView on the next line. So here are my first two lines now:

class SettingsController: UIViewController {
  var tableView = UITableView()

进一步,我有这段代码将TableView附加到 UIView :

Further down, I have this code to attach the TableView to the UIView:

override func viewDidLoad() {
    super.viewDidLoad()   
    self.view.addSubview(tableView)
}

该应用程序成功构建,但是在尝试访问该视图时,屏幕为空白,因此我还无法尝试约束部分(如果我还原描述的内容,屏幕会正确显示数据)

The app builds successfully but when trying to access this view, the screen is blank, so I've not been able to attempt the constraints part yet (the screen does display the data correctly if I revert what I've described).

我在该类中的许多现有函数都引用了 tableView 来传递数据/设置行数等,但是看来我做得还不对...我想念吗?

Many of my existing functions within this class reference tableView for passing in the data/setting the number of rows etc, but it seems I've not done something right... is there a piece I'm missing?

推荐答案

这是一个以编程方式创建表视图并将其添加到普通"视图的简单示例:

Here is a simple example of programmatically creating and adding a table view to a "normal" view:

//
//  SettingsController.swift
//
//  Created by Don Mag on 7/25/17.
//

import UIKit

class SettingsController : UIViewController, UITableViewDelegate, UITableViewDataSource {

    let tableView : UITableView = {
        let t = UITableView()
        t.translatesAutoresizingMaskIntoConstraints = false
        return t
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // set a background color so we can easily see the table
        self.view.backgroundColor = UIColor.blue

        // add the table view to self.view
        self.view.addSubview(tableView)

        // constrain the table view to 120-pts on the top,
        //  32-pts on left, right and bottom (just to demonstrate size/position)

        tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 32.0).isActive = true
        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 120.0).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -32.0).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32.0).isActive = true

        // set delegate and datasource
        tableView.delegate = self
        tableView.dataSource = self

        // register a defalut cell
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    // Note: because this is NOT a subclassed UITableViewController, 
    // DataSource and Delegate functions are NOT overridden

    // MARK: - Table view data source

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 25
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = "\(indexPath)"

        return cell
    }

    // MARK: - Table view delegate

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // etc
    }

}

应该无需编辑即可运行此程序,并获得如下结果:这是一个非常简单的示例,因此应该很容易看到正在发生的事情.

You should be able to run this without edits, and get a result along these lines: It is a very simple example, so should be pretty easy to see what's going on.

如果您已经在使用(我假设是,复杂的)设置 UITableViewController 类,那么遵循该框架并将您的框架转换为 UIViewController 应该很简单代码> + UITableView-as-subview .

If you already have your (I'm assuming, complex) Settings UITableViewController class working, it should be pretty straight-forward to follow this skeleton and convert yours to a UIViewController + UITableView-as-subview.

或者,您可以加载现有的 UITableViewController 作为子视图控制器,并将其视图(表视图)添加为子视图.

Alternatively, you can load your existing UITableViewController as a child view controller, and add its view (the tableview) as a subview.

这篇关于如何将编程约束添加到UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆