如何在当前单元格(翠鸟)设置图像? Swift
How to set Image at current Cell (Kingfisher)? Swift
我有一个带有自定义单元格的 TableView
。标签 smiles
包含 links.
如何将 link 中的图像放到当前 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
),因此当您将单元格出队时,您有责任 update/reset UI
元素根据每个单元格的数据。
我有一个带有自定义单元格的 TableView
。标签 smiles
包含 links.
如何将 link 中的图像放到当前 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
),因此当您将单元格出队时,您有责任 update/reset UI
元素根据每个单元格的数据。