首页 > 解决方案 > TableViewCell Swift 中的自定义视图

问题描述

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



    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
    }

}

在此处输入图像描述

当我运行下面的应用程序时,它是如何显示的屏幕截图,请在此处输入图像描述

在此处输入图像描述

问题 1:第 3 行是自定义视图,其背景应为自定义单元格类中设置的蓝色

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

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

请指教。我被这里打动了

标签: iosswiftuitableviewview

解决方案


添加你cellContentView的单元格contentview

Custom Tablecell:
  class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var cellContentView: CustomView!

      override func awakeFromNib() {
        super.awakeFromNib()

        // Initialization code
        contentView.addSubview(cellContentView) // add this line
        cellContentView.backgroundColor = UIColor.blue
    }

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

        // Configure the view for the selected state
    }

}

推荐阅读