Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 访问外部单元格时获取零_Ios_Uitableview_Swift3 - Fatal编程技术网

Ios 访问外部单元格时获取零

Ios 访问外部单元格时获取零,ios,uitableview,swift3,Ios,Uitableview,Swift3,下面是我的代码,我正在函数中访问单元格,但我得到的是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

下面是我的代码,我正在函数中访问
单元格
,但我得到的是
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

    }
}

使用dequeueReusableCell

let cell = tblView.dequeueReusableCell(withIdentifier: "CustomLocateVehicleCell") as! CustomLocateVehicleCell
cell.configureCell(stausText, statusColor)
  • 但在此之前,请确保在UITableView上拖放UITableViewCell
  • 使用CustomLocateVehicleCell更改单元格的类别
  • 设置标识符CustomLocateVehicleCell
  • configureCell在CustomLocateVehicleCell中创建此方法,并将代码写入托管单元格的颜色和文本位置

  • 通过传递
    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
    
    }
    

    您只能获取屏幕上可见的单元格。1。细胞在屏幕上可见吗?2是否为
    CustomLocateVehicleCell
    类型的单元格?@ozgur 1.是2.是否为
    CustomLocateVehicleCell
    类型的单元格?@ReinierMelian是