1

I have an TableView with custom cells. Label smiles contain links.

How can I put Image from link to current ImageView'cell? My code

 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)
        } 

***
}

not working. I'm getting error for line self.cell![indexPath.row].smile.kf.setImage(with: url)

Type 'customChatCell' has no subscript members

I'm using Kingfisher. If I use code

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

image putting into all cells, not for current.

Please help me fix it.

  • Why do you use the property `self.cell` and not simply `let cell = ... dequeue ...`? Also, what do you want do achieve with `cell![indexPath.row]`? Looks like an array access to me. – Andreas Oetjen Dec 19 '18 at 08:32
  • I want simple download image from url contains in label (let text) and put it to ImageView smile (to current cell). Sorry for noob question – Артем Ильинский Dec 19 '18 at 08:37
  • Could you please let us know what do you mean by current cell? – Gaurav Tiwari Dec 19 '18 at 08:40
  • In my TableView non-static count of cells. All cells contains text-label and imageview. But only in few cells text-label contains link for image. I want to put image from text-label to cell, which contains link in text-label. Not for all, only for this few cells. – Артем Ильинский Dec 19 '18 at 08:44

1 Answers1

2

You should remove keeping the cell reference at class level. Your cellForRow should look like this,

 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  
       }
} 

Remember, you are using a single UIView(i.e, customChatCell) for each cell in UITableView so when you dequeue a cell it's your responsibility to update/reset the UI elements according to your data for each cell.

Kamran
  • 14,987
  • 4
  • 33
  • 51
  • In different cells text-label can contains different links. With your code image from last link putting into all cells. – Артем Ильинский Dec 19 '18 at 08:59
  • Because i don't know what is `message`? If `message` is same for all cells then you should see the same `image`. You should print `text` and verify you are getting different `text` for each `cell`. – Kamran Dec 19 '18 at 09:00