【问题标题】:how to add custom view on UITableViewCell programatically?如何以编程方式在 UITableViewCell 中添加自定义视图?
【发布时间】:2019-04-06 23:36:50
【问题描述】:

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

    }

}

除此之外我还尝试过:使用普通的 addSubiew 和约束到单元格的 self 锚点,并添加到 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

在桌子上

【问题讨论】:

    标签: ios swift uitableview uikit


    【解决方案1】:

    在我看来,您的单元类中的代码看起来没有什么问题。你能添加你的tableviewcode吗?

    【讨论】:

    • 我看到你投了 view.translatesAutoresizingMaskIntoConstraints = false 两次。尝试在 setupViews() 中删除那个
    • 我尝试运行您的代码(除了类 Switch),它在为我删除 setupViews 中的 autordranslte 时有效。我希望这样做可以解决问题!
    【解决方案2】:

    你应该实现

    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)
    ])  
    

    【讨论】:

    • 这会是self.translatesAutoresizingMaskIntoConstraints = false 代码中的另一个问题吗?我认为它会忽略高度限制。
    • Sh_Khan,两种方法都试过了,但由于某种原因,它们都不起作用,怀疑在声明表时出现问题,附加代码,我将不胜感激得到任何帮助或提示
    • 谢谢您,在您的帮助下,通过设置rowHeight = UITableViewAutomaticDimension 和固定estimatedRowHeight = 65 解决了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多