如何使用表视图单元格外部视图功能



我需要使用视图在单元格中更改图像。但是我看不到我的单元将出现在这里我所做的

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    let cell: HomeCellTableViewCell = self.tableCity.dequeueReusableCell(withIdentifier: "Cell") as! HomeCellTableViewCell
    if session == nil {
        print("first")
        cell.iconForDownload.setImage(UIImage(named: "ic_download"), for: .normal)
    } else {
        print("second")
        cell.iconForDownload.setImage(UIImage(named: "ic_next_green"), for: .normal)
    }
}

它打印"第一",但图像仍然没有更改

在我的cellForRowAt中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: HomeCellTableViewCell = self.tableCity.dequeueReusableCell(withIdentifier: "Cell") as! HomeCellTableViewCell
    let city = listData[indexPath.row]
    cell.labelNameCity.text = city.region
    cell.labelNameCityJpn.text = city.regionJpn
    let stringImage = config.BASE_URL+city.imgArea
    let url = URL(string: stringImage.replacingOccurrences(of: " ", with: "%20"))
    urlDownload = config.BASE_URL+kota.urlDownload
    urlDownloadFinal = URL(string: urlDownload.replacingOccurrences(of: " ", with: "%20"))
    if session == nil {
        cell.imageCity.kf.setImage(with: url)
        cell.iconForDownload.setImage(UIImage(named: "ic_download"), for: .normal)
    } else {
        cell.imageCity.kf.setImage(with: url)
        cell.iconForDownload.setImage(UIImage(named: "ic_next_green"), for: .normal)
    }
    return cell
}

您需要使用cellForRow(at:)获得cell,它将返回可选的UITableViewCell,因此请使用if letguard let包装可选的。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    let indexPath = IndexPath(row: 0, section: 0) //Set your row and section
    if let cell = tableView.cellForRow(at: indexPath) as? HomeCellTableViewCell {
        //access cell
    }
}

注意:击球方法是设置数据源数组,然后使用reloadRows(at:with:)重新加载受影响的表行。

let indexPath = IndexPath(row: 0, section: 0) //Set your row and section
self.tableView.reloadRows(at: [indexPath], with: .automatic)

最新更新