繁体   English   中英

UIView内的UITableView

[英]UITableView inside UIView

我正在尝试在xib中创建动态UITableView,所以我认为可行的方法是将表视图放入空白UIView中。 然后,我将对该UIView进行子类化,使其遵循UITableViewDelegateUITableViewDataSource协议。 有人可以指导我吗,因为我尝试了很多事情,但是没有一个起作用。 非常感谢!

编辑:

我将向您展示我之前尝试过的内容(对不起,没有原始代码):

class TableControllerView: UIView, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        tableView.frame = self.frame

        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(...cellForRowAt...) {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)

        cell.titleLabel?.text = "test title"

        return cell
    }
}

但是在这种情况下,即使我将其框架设置为与视图相同,表视图最终还是变得太大且与视图不对齐

编辑2:

我的代码现在看起来像这样:

class PharmacyTableView: UIView, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var pharmacyTableView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //PROVISOIRE depends on user [medications].count
    return 2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)

    cell.textLabel?.text = "text label test"
    cell.detailTextLabel?.text = "detail label test"

    return cell
}}

表格视图已初始化,但仅在限制高度锚点时显示,我不希望这样做,因为它可能会根据用户数据而增大或缩小。 我想解决这个问题后我会完成吗? PS:另外,非常感谢那些花时间帮助我的人:)

编辑3:

因此,我将表视图的类更改为:

class IntrinsicResizingTableView: UITableView {

override var contentSize:CGSize {
    didSet {
        self.invalidateIntrinsicContentSize()
    }
}

override var intrinsicContentSize: CGSize {
    self.layoutIfNeeded()
    return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}

}

现在一切正常! 最后!

您的问题不清楚。 根据您的最终声明,以下代码将用于保持表格视图及其父视图对齐。

tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

如果这不是您要找的答案,请说明您的问题。 如果您解释您要做什么,这也会有所帮助吗? 为什么需要这个超级视图?

暂无
暂无

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

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