在访问外部单元时获取零



以下是我的代码,我正在访问我的功能中的 cell,但是我正在获取nil

 private func setDataForStatusLabel(stausText: String, statusColor: UIColor) {
    let indexPath = IndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath) as? CustomLocateVehicleCell
    if let cell = cell {
        cell.statusLabel.text = stausText
        cell.containerStatusLabel.text = stausText
        cell.statusLabel.textColor = statusColor
        cell.containerStatusLabel.textColor = statusColor
        cell.statusColorView.backgroundColor = statusColor
        cell.containerStatusColorView.backgroundColor = statusColor
    }
}

最终通过传递cell参数

解决了问题
private func setDataForStatusLabel(stausText: String, statusColor: UIColor, cell: CustomLocateVehicleCell) {
    cell.statusLabel.text = stausText
    cell.containerStatusLabel.text = stausText
    cell.statusLabel.textColor = statusColor
    cell.containerStatusLabel.textColor = statusColor
    cell.statusColorView.backgroundColor = statusColor
    cell.containerStatusColorView.backgroundColor = statusColor
}

使用Dequeuereusablecell

let cell = tblView.dequeueReusableCell(withIdentifier: "CustomLocateVehicleCell") as! CustomLocateVehicleCell
cell.configureCell(stausText, statusColor)
  1. ,但在此之前,请确保将UitaiteViewCell拖放并丢弃在UITATION VIEW上
  2. 使用CustomLocatovehicleCell
  3. 更改单元格类
  4. 设置标识符CustomLocateVehicleCell
  5. configurecell在CustomLocateLocateVehicleCell中创建此方法,并在托管单元的颜色和文本
  6. 的位置编写您的代码

最新更新