具有嵌入式视图控制器自定义大小的UITableView单元格?

时间:2019-06-11 04:44:36

标签: ios swift uitableview

我正在尝试使用嵌入式视图控制器,以使我的代码保持整洁,并且仅做一小部分功能。因此,我已经朝着将视图控制器嵌入表视图单元内部的方向(全部用代码编写)的方向进行了说明,但是我无法根据视图控制器中的内容使单元格达到自己的大小,对您有所帮助吗? >

这是表格视图单元格的相关代码:

final class ViewControllerContainedTableViewCell: UITableViewCell {

    private lazy var contentViewController: UIViewController = {
        let viewController = UIViewController()
        viewController.view.translatesAutoresizingMaskIntoConstraints = false
        return viewController
    }()

    func addContentViewController(parent: UIViewController?) {
        contentViewController.willMove(toParent: parent)
        parent?.addChild(contentViewController)
        contentView.addSubview(contentViewController.view)

        NSLayoutConstraint.activate([
            contentViewController.view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            contentViewController.view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            contentViewController.view.topAnchor.constraint(equalTo: contentView.topAnchor),
            contentViewController.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
            ])

        contentViewController.didMove(toParent: parent)
    }

    func removeContentViewController() {
        contentViewController.willMove(toParent: nil)
        contentViewController.view.removeFromSuperview()
        contentViewController.removeFromParent()
    }

}

我分别在addContentViewController(parent: UIViewController?)removeContentViewController()的数据源中调用func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)

这是视图控制器的相关代码(示例):

final class ContentViewController: UIViewController {

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.text = "Title"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupLayout()
    }

    private func setupLayout() {
        view.addSubview(titleLabel)

        NSLayoutConstraint.activate([
            titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            titleLabel.topAnchor.constraint(equalTo: view.topAnchor),
            titleLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor)
            ])
    }

}

我希望我的单元格能够适应标题标签的大小,但是单元格保持44pt的高度。

您可以提供任何帮助或指导吗?我是否正确使用/嵌入了视图控制器?我在做什么错了?

谢谢!

1 个答案:

答案 0 :(得分:0)

因此,我尝试了您的示例并对其进行了一些编辑,以使视图控制器正常工作。您的设置似乎可以正常工作。

以下是完整的代码,您可以粘贴到新项目“ ViewController.swift”中。在情节提要中添加了一个表格视图,并设置了具有委托的数据源。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet private var tableView: UITableView?

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ViewControllerContainedTableViewCell()
        cell.parentViewController = self
        cell.setContentViewController(controller: {
            let controller = ContentViewController()
            controller.view.translatesAutoresizingMaskIntoConstraints = false
            if indexPath.row%2 == 0 {
                controller.text = "Some fairly long text to get a bit of a feeling as to what is going on here"
                controller.view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            } else {
                controller.text = "Some fairly long text to get a bit of a feeling as to what is going on here Some fairly long text to get a bit of a feeling as to what is going on here Some fairly long text to get a bit of a feeling as to what is going on here Some fairly long text to get a bit of a feeling as to what is going on here"
            }
            return controller
        }())
        return cell
    }

}

final class ViewControllerContainedTableViewCell: UITableViewCell {

    var parentViewController: UIViewController?
    var childViewController: UIViewController?


    func setContentViewController(controller: UIViewController?) {
        guard let parentViewController = parentViewController else {
            print("No parent controller set!")
            return
        }

        removeContentViewController()
        childViewController = controller

        if let controller = controller {
            controller.willMove(toParent: parentViewController)
            parentViewController.addChild(controller)
            contentView.addSubview(controller.view)

            NSLayoutConstraint.activate([
                controller.view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
                controller.view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
                controller.view.topAnchor.constraint(equalTo: contentView.topAnchor),
                controller.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
                ])

            controller.didMove(toParent: parentViewController)
        }


    }

    func removeContentViewController() {
        guard let childViewController = childViewController else { return }
        childViewController.willMove(toParent: nil)
        childViewController.view.removeFromSuperview()
        childViewController.removeFromParent()
    }

}

final class ContentViewController: UIViewController {

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.text = "Title"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    var text: String? {
        didSet {
            titleLabel.text = text
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupLayout()
        titleLabel.text = self.text
    }

    private func setupLayout() {
        view.addSubview(titleLabel)

        NSLayoutConstraint.activate([
            titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            titleLabel.topAnchor.constraint(equalTo: view.topAnchor),
            titleLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor)
            ])
    }

}

这是结果:

enter image description here