如何以编程方式在UITableViewCell上添加自定义视图?

时间:2019-04-06 23:36:50

标签: ios swift uitableview uikit

UPD已解决-参见下面的修改后问题

试图向UITableViewCell的自定义子类添加自定义视图(具体来说是按钮),但无法在设备上的iOS 10.1上看到任何布局结果。没有看到版式上的任何更改,而是尝试使用红色背景的自定义视图填充单元格,但也未能实现此结果。

import UIKit

class Save: UITableViewCell {

    let containerView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .red
        return view
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setupViews() {
        self.translatesAutoresizingMaskIntoConstraints = false
        addSubview(containerView)
        containerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        containerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        containerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

    }

}

除此之外,我还尝试了以下操作:对单元格的self锚点使用普通的addSubiew和约束,并在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)中添加到contentView中,但是两种方法都没有执行任何操作,但该单元格显示为空。

UPD 附加TableView初始化代码以供参考

class SettingsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    let settings = [
        "setting1",
        "setting2",
        "setting3",
        "setting4",
        "setting5",
        "setting6",
        "setting7"
    ]


    let overlay: UIView = {
        var view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .gray
        view.alpha = 0.3
        view.isHidden = true
        return view
    }()



    let tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "commonCell")
        tableView.register(Save.self, forCellReuseIdentifier: "btnCell")
        tableView.register(Switcher.self, forCellReuseIdentifier: "switcherCell")
        tableView.headerView(forSection: 0)?.textLabel?.text = "settings of app"
        tableView.tableFooterView = UIView()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = UITableViewAutomaticDimension
        tableView.allowsSelection = false
        return tableView
    }()

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

    func setupViews() {
        view.backgroundColor = .white
        view.addSubview(tableView)

        tableView.dataSource = self
        tableView.delegate = self

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


    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        tabBarController?.navigationItem.rightBarButtonItems = []
        tabBarController?.navigationItem.title = "settings"
    }

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

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

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "settings"
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "commonCell", for: indexPath)
        cell.textLabel?.text = settings[indexPath.row]
        if(indexPath.row == 6) {
            cell.textLabel?.text = ""
            cell = tableView.dequeueReusableCell(withIdentifier: "btnCell", for: indexPath)
        }
        return cell
    }


}

已解决

除了“ Sh_Khan”回答外,还需要设置

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 65

在桌子上

2 个答案:

答案 0 :(得分:1)

在我看来,您的单元格类中的代码似乎没有任何问题。您能添加您的tableviewcode吗?

答案 1 :(得分:0)

您应该实现

heightForRowAt

或添加

containerView.heightAnchor.constraint(equalToConstant:200).isActive = true

还将任何子视图添加到contentView

contentView.addSubview(containerView)
NSLayoutConstraint.activate([ 
    containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
    containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
    containerView.topAnchor.constraint(equalTo: contentView.topAnchor),
    containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
    containerView.heightAnchor.constraint(equalToConstant:200)
])  
相关问题