ios - UITableViewCell Swift 中的自定义 View

标签 ios swift uitableview view

我是 swift 的新手,下面是我遇到的问题。我有一个 TableView ,其中一个单元格是 customCell,它加载自定义 View 。没有崩溃,但不显示表格单元格的约束和背景颜色。请引用下面的代码



    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 行是自定义 View ,其背景应为自定义单元格类中设置的蓝色

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

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

请指教。我在这里被打动了

最佳答案

在单元格 contentview 中添加您的 cellContentView

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
    }

}

关于ios - UITableViewCell Swift 中的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59639150/

相关文章:

ios - 为什么数据不在 View Controller 之间传递? swift 3

iphone - 使用嵌套 JSON 填充 UITableView

ios - 将 NSArray 保存到 NSUserDefaults 不起作用

ios - Swift 中的语音转字符串

ios - swift 1.2 : GAI -> Cannot invoke 'send' with an argument list of type '(NSMutableDictionary!)'

swift - 通过 KeyWindow 将 XIB 作为 UIView 呈现和删除

ios - 如何访问 block 外部的变量?

ios - Disqus SSO, "Your API key is not valid on this domain"

ios - RxDataSources itemDeselected 没有被调用

ios - 来自 NSString 的 UITableViewCell Nib