从自定义视图加载表格单元内容

问题描述 投票:0回答:1

swift我是新手,以下是我面临的问题我有一个tableview,单元格之一是customCell,它加载自定义视图。没有崩溃,但未显示表格单元的约束和背景色。 请参阅下面的代码

class ViewController: UIViewController, UITableViewDataSource {


    let data = ["Apple", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape"]
    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var customView: CustomView!
    override func viewDidLoad() {
        super.viewDidLoad()
        customView.myLabel.text = "Hey hey"
        customView.backgroundColor = UIColor.green
        tableView.dataSource = self
        tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.tableFooterView = UIView()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 3 {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomTableViewCell else { return UITableViewCell()}

//            cell.layoutSubviews()
//            cell.layoutIfNeeded()
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = data[indexPath.row]
            return cell
        }


    }
}


Custom View:
class CustomView: UIView {

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var myLabel: UILabel!

    let kCONTENT_XIB_NAME = "CustomView"
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    private func commonInit() {
        Bundle.main.loadNibNamed(kCONTENT_XIB_NAME, owner: self, options: nil)
        contentView.fixInView(self)
    }

}

extension UIView
{
    func fixInView(_ container: UIView!) -> Void{
        self.translatesAutoresizingMaskIntoConstraints = false;
        self.frame = container.frame;
        container.addSubview(self);
        NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
    }
}

Custom Tablecell:
class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var cellContentView: CustomView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        cellContentView.backgroundColor = UIColor.blue
    }

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

        // Configure the view for the selected state
    }

}

下面是情节提要结构enter image description here

[当我运行以下应用程序时,该屏幕截图是如何显示的enter image description here

问题1:第3行是customview,其背景应为蓝色(如在自定义单元格类中设置的那样)

问题2:在我的视图控制器中,即使将背景色设置为绿色后,为什么它仍不显示该颜色。创建视图时在情节提要中使用“橙色”

问题3:如果我有更多的动态控件,如何设置像元高度。

请咨询。我被打中了

ios swift uitableview custom-view tablecell
1个回答
0
投票

对于问题1,您必须添加自定义视图作为单元格的内容视图。只需添加以下行:

contentView.addSubview(cellContentView)
cellContentView.backgroundColor = UIColor.blue
© www.soinside.com 2019 - 2024. All rights reserved.