设置UITableView的宽度等于里面的最大单元格宽度吗?

时间:2019-08-01 21:29:05

标签: swift xcode uitableview constraints

我创建了一个自定义UITableViewCell类,如下所示:

import UIKit

class SortByMenuTableViewCell: UITableViewCell {

    var cellTextLabel: UILabel = {

        let label = UILabel()

        label.numberOfLines = 1

        label.textAlignment = .left

        label.translatesAutoresizingMaskIntoConstraints = false

        return label

    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(cellTextLabel)

        NSLayoutConstraint.activate([

            cellTextLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10),

            cellTextLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10),

            cellTextLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10)

        ])

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    override func awakeFromNib() {

        super.awakeFromNib()

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

}

然后我将上面的tableViewCell添加到下面的ViewController中,该ViewController包含UITableView元素:

    import UIKit

    class SortByPopOverVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

        var tableViewRowsInsideSectionsArray = ["Section Designation","Depth, h","Width, b","Area of Section, A"]

        var sortByTableView = UITableView()

        var cellsWidthsArray: [CGFloat] = []

        override func viewDidLoad() {

            super.viewDidLoad()

            sortByTableView.delegate = self

            sortByTableView.dataSource = self

            sortByTableView.translatesAutoresizingMaskIntoConstraints = false

            sortByTableView.register(SortByMenuTableViewCell.self, forCellReuseIdentifier: "customCell")

            sortByTableView.isScrollEnabled = false

            view.addSubview(sortByTableView)

        }

        override func viewWillLayoutSubviews() {            

            NSLayoutConstraint.activate([

                sortByTableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),

                sortByTableView.leftAnchor.constraint(equalTo: self.view.leftAnchor),

                sortByTableView.bottomAnchor.constraint(equalTo: sortByTableView.topAnchor, constant: sortByTableView.contentSize.height),

                sortByTableView.widthAnchor.constraint(equalToConstant: 200)

                ])

sortByTableView.widthAnchor.constraint(equalToConstant: cellsWidthsArray.max()!).isActive = true

            print("Cell widths array = \(cellsWidthsArray)")

        }


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

            return 1

        }

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

            return tableViewRowsInsideSectionsArray.count

        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = sortByTableView.dequeueReusableCell(withIdentifier: "customCell") as! SortByMenuTableViewCell

            cell.cellTextLabel.text = tableViewRowsInsideSectionsArray[indexPath.row]

            cell.cellTextLabel.sizeToFit()

            if cell.cellTextLabel.frame.size.width != 0 {

               cellsWidthsArray.append(cell.cellTextLabel.frame.size.width)

            }

            return cell

        }

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

            dismiss(animated: true, completion: nil)

        }

    }

从上面的UIViewController代码中可以看出,我创建了一个Array,它将保存TableView中包含的所有单元格宽度的值。之后我要实现的是将UITableVIew的宽度设置为等于Array内部的最大单元宽度。

但是,当我在控制台中打印出Array的值以检查是否已将单元格宽度附加到Array时,我才发现在那找到了它们。但是,当我尝试将widthAnchor再设置一次等于数组中的最大值时,我收到一条错误消息,说“展开时未找到”

我如何将UITableView的宽度设置为等于其内部单元格的最大宽度,对您有所帮助吗?

0 个答案:

没有答案
相关问题