如何将程序性约束添加到UITableView?

问题描述 投票:2回答:3

我有一个课程设置如下:

class SettingsController: UITableViewController {

我希望能够为UITableView添加约束,以便在表的两侧有相等的间距(它当前是一个全宽度的表,所以在iPad上有点难看)。

我已经读过我不能向UITableViewController添加约束,而是我必须在我的类中添加一个UIViewController,添加一个UITableView,然后我应该能够将约束添加到TableView。

我修改了课程并在下一行定义了UITableView。所以这是我现在的前两行:

class SettingsController: UIViewController {
  var tableView = UITableView()

再往下,我有这个代码将TableView附加到UIView

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

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

我在这个类中的许多现有函数引用tableView来传递数据/设置行数等,但似乎我没有做正确的事情......是否有一件我错过了?

ios swift uitableview constraints
3个回答
6
投票

以下是以编程方式创建表视图并将其添加到“普通”视图的简单示例:

//
//  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
    }

}

你应该能够在没有编辑的情况下运行它,并得到这些方面的结果:enter image description here这是一个非常简单的例子,所以应该很容易看到发生了什么。

如果您已经拥有(我假设,复杂)设置UITableViewController类工作,那么应该非常直接地遵循这个骨架并将您的转换为UIViewController + UITableView-as-subview

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


3
投票

所以,看起来你需要备份一个步骤而不是试图以某种方式在TableViewController中嵌入一个UIViewController。你提到的建议意味着你的SettingsController应该是UIViewController的子类,你应该为它添加一个UITableView。

向UITableView添加约束会比尝试使用UITableViewController更好。

编辑

在获得任何数据之前,您应该能够实现约束。您只会看到一个空表视图,但它仍会显示行。如果你能看到你正在处理的东西,有时候玩约束会更容易。当您创建tableView时,您可以使用:var tableView = UITableView(frame: self.view)来帮助您获得轴承。

因此,在实现了所需的约束之后,看起来您将缺少的是设置tableView的委托和dataSource并扩展视图控制器以实现这些方法。然后你应该有数据,你可以沉浸在你辛勤工作的荣耀中。


1
投票

我只是展示了另一种添加约束的方法。

首先,您需要设置view.translatesAutoresizingMaskIntoConstraints = false并执行以下操作

// align tableView from the left and right
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[view]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": tableView]))

// align tableView from the top and bottom
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[view]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": tableView]))

它将针对self.view(它的superview)给表视图提供top,bottom,leading和trailing约束。

© www.soinside.com 2019 - 2024. All rights reserved.