首页 > 解决方案 > UITableView 没有在我的自定义单元格 UILabel 中正确显示数据和重用问题

问题描述

我创建了我的自定义单元格类并使用代码和约束设计了我的用户界面(我有 2 个UILabels在每一行中保存我的数据)。我在主viewController中注册了我的customcell类,并在.tableview

问题是我UILabels的数据填充之一,但第二个不显示它的数据,直到我滚动表格视图或在该行上单击右键。见图像。

滚动前

在此处输入图像描述

滚动后

在此处输入图像描述

这是我的代码:

class ProjectDetailsCustomCell: UITableViewCell {
    var key   : String?
    var value : String?

    var keyContainerView : UIView = {
        var containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.backgroundColor = UIColor(red: 0.867, green: 0.867, blue: 0.867, alpha: CGFloat(1))
        return containerView
    }()

    var keyView : UILabel = {
        var keyTextView = UILabel()
        keyTextView.translatesAutoresizingMaskIntoConstraints = false
        keyTextView.font = UIFont(name: "IRANSansWeb", size: 13)
        keyTextView.textAlignment = .center
        return keyTextView
    }()

    var valueView : UILabel = {
        var valueTextView = UILabel()
        valueTextView.translatesAutoresizingMaskIntoConstraints = false
        valueTextView.textAlignment = .center
        valueTextView.font = UIFont(name: "IRANSansWeb", size: 12)
        //valueTextView.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: CGFloat(1))
        return valueTextView
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(valueView)
        self.addSubview(keyContainerView)
        keyContainerView.addSubview(keyView)
        self.backgroundColor = UIColor.clear
        self.heightAnchor.constraint(equalToConstant: 42).isActive = true
        keyContainerView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        keyContainerView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        keyContainerView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.4).isActive = true
        keyContainerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
        keyView.rightAnchor.constraint(equalTo: self.keyContainerView.rightAnchor).isActive = true
        keyView.topAnchor.constraint(equalTo: self.keyContainerView.topAnchor).isActive = true
        keyView.bottomAnchor.constraint(equalTo: self.keyContainerView.bottomAnchor).isActive = true
        keyView.widthAnchor.constraint(equalTo: self.keyContainerView.widthAnchor).isActive = true
        valueView.rightAnchor.constraint(equalTo: self.keyContainerView.leftAnchor).isActive = true
        valueView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        valueView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        valueView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.6).isActive = true
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if let key = key {
            keyView.text = key + value!
        }

        if let value = value {
            valueView.text = value
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

这是我填充数据数组的数据源代码:

for i in 0..<self.detailsArray.count {
            // Populate our array list with the real data and restart the tableview
            self.dict = self.detailsArray[i]
            self.dataList.append(DetailsCellData.init(key: "شماره سفارش", value: String(self.dict["Id"] as! Int)))
            self.dataList.append(DetailsCellData.init(key: "قیمت", value: self.dict["Price"] as? String))
            self.dataList.append(DetailsCellData.init(key: "زمان و تاریخ سفارش", value: self.dict["DateTime"] as? String))
            self.dataList.append(DetailsCellData.init(key: "نام و نام خانوادگی کارفرما", value: self.dict["UserInformation"] as? String))
            self.dataList.append(DetailsCellData.init(key: "شماره تماس کارفرما", value: self.dict["UserPhone"] as? String))
            self.dataList.append(DetailsCellData.init(key: "نام خدمات دهنده", value: self.dict["ContractorInformation"] as? String))
            self.dataList.append(DetailsCellData.init(key: "شماره خدمات دهنده", value: self.dict["ContractorPhone"] as? String))
            self.dataList.append(DetailsCellData.init(key: "تاریخ درخواست خدمات", value: self.dict["ServiceDate"] as? String))
            self.dataList.append(DetailsCellData.init(key: "ساعت درخواست خدمات", value: self.dict["ServiceTime"] as? String))
            self.dataList.append(DetailsCellData.init(key: "مقدار کار", value: (self.dict["WorkAmmount"] as? String)! + " " + (self.dict["WorkUnit"] as? String)!))
            self.dataList.append(DetailsCellData.init(key: "هزینه تخمینی (ریال)", value: self.dict["EstimatedPrice"] as? String))
            self.tableview.reloadData()

这是我的表格视图代表代码

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (dataList.count < 11){
        return 0
    } else {
        return dataList.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "detailCustomCell", for: indexPath) as! ProjectDetailsCustomCell
    cell.key = self.dataList[indexPath.row].key
    cell.value = self.dataList[indexPath.row].value
    return cell
}

标签: iosswiftuitableviewcustom-cell

解决方案


override func layoutSubviews() {
    super.layoutSubviews()

    updateLayout()
}

func updateLayout() {
    if let key = key {
        keyView.text = key + value!
    }

    if let value = value {
        valueView.text = value
    }
}

试试这个方法,这里也加一行

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "detailCustomCell", for: indexPath) as! ProjectDetailsCustomCell
    cell.key = self.dataList[indexPath.row].key
    cell.value = self.dataList[indexPath.row].value
    cell.updateLayout()
    return cell
}

推荐阅读