TableViewCell Swift

时间:2020-01-08 03:46:20

标签: ios swift uitableview view

我是新手,下面是我面临的问题。我有一个tableview,单元格之一是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
    }

}

enter image description here

当我运行以下应用程序时,屏幕截图显示了该屏幕快照的输入方式,在此处输入图像描述

enter image description here

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

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

问题3:如果要查看更多动态控件,如何设置像元高度。

请咨询。我在这里被击中

1 个答案:

答案 0 :(得分:0)

将您的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
    }

}
相关问题