gpt4 book ai didi

swift - 如何在当前单元格(翠鸟)设置图像? swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:22 25 4
gpt4 key购买 nike

我有一个带有自定义单元格的 TableView。标签 smiles 包含链接。

如何将链接中的图像放到当前 ImageView 的单元格中?我的代码

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "ClientCell"
self.cell = self.tableView.dequeueReusableCell(withIdentifier: identifier) as? customChatCell

let text = message[Constants.MessageFields.text] ?? ""
let selectedCell = self.tableView.cellForRow(at: indexPath) as? customChatCell

***

if text.range(of:"smiles") != nil {
let url = URL(string: text)
self.cell![indexPath.row].smile.kf.setImage(with: url)
}

***
}

不工作。 self.cell![indexPath.row].smile.kf.setImage(with: url)

行出现错误

Type 'customChatCell' has no subscript members

我正在使用翠鸟。如果我使用代码

self.cell.smile.kf.setImage(with: url)

图像放入所有单元格,不用于当前。

请帮我修复它。

最佳答案

您应该删除将 cell 引用保持在 class 级别。你的 cellForRow 应该是这样的,

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "ClientCell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? customChatCell

let text = message[Constants.MessageFields.text] ?? ""
if text.range(of:"smiles") != nil {
let url = URL(string: text)
cell.smile.kf.setImage(with: url)
} else {
// Reset image to nil here if it has no url
cell.smile.image = nil
}
}

请记住,您正在为 UITableView 中的每个单元格使用单个 UIView(即 customChatCell),因此当您出列一个单元格时,它是您的负责根据每个单元格的数据更新/重置 UI 元素。

关于swift - 如何在当前单元格(翠鸟)设置图像? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53847153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com