ios - 如何以编程方式在 UITableViewCell 中添加自定义 View ?

标签 ios swift uitableview uikit

UPD 已解决 - 请参阅下面已编辑的问题

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

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

    }

}

除此之外我还尝试过:使用简单的 addSubiew 和对单元格的 self anchor 的约束,并添加到 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 但是这两种方法都没有做任何事情,单元格出现了但是它是空的。

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 answer,还需要设置

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 65

在 table 上

最佳答案

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

关于ios - 如何以编程方式在 UITableViewCell 中添加自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554391/

相关文章:

ios - Swift - 在 vi​​ewDidLoad() 中完成 http 请求之前查看负载

ios - UitableView 下移

ios - 我可以为 iOS 应用程序和网站使用一个解析数据库吗? - 使用 swift

ios - heightForRowAt 与 estimatedHeightForRowAt 有什么区别?

ios - 如何通过按钮生成随机的 6 位数字?

ios - 视障用户无需点击屏幕即可进行语音输入

ios - 自定义 Segue - 无法识别的选择器发送到实例 - setUseDefaultModalPresentationStyle

iphone - ViewController 上的本地通知

swift - url 路径中的 Alamofire 变量或参数支持

swift - 尝试将事件标签用作标题时发生崩溃