0

I never get this print . it is like is always nil, doesnt matter how much i scroll it up or down :/

if let update =  tableView.cellForRow(at: indexPath ) as? RestCell {
                print("VISIBLE CELL")
                }

Complete code..

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell : RestCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RestCell

        // Pass data to Cell :) clean the mess at the View Controller ;)
        cell.restData = items[indexPath.row]
        // Send cell so it can check update the image to the right cell ;)
       // cell.cell = tableView.cellForRow(at: indexPath ) as? RestCell
        //print("LA CELDA ES \(tableView.cellForRow(at: indexPath ))")
        if let update =  tableView.cellForRow(at: indexPath ) as? RestCell {
        print("VISIBLE CELL")
        }

        return cell
    }
lorenzo gonzalez
  • 1,894
  • 4
  • 14
  • 18
  • Why would you be calling `cellForRow(at:)` inside the `cellForRowAt` data source method? What are you trying to achieve with that line? – rmaddy Oct 25 '17 at 17:47
  • `cell` **is** the cell you are looking for. – vadian Oct 25 '17 at 17:48
  • i was kindof thinking on that.. make sense, can you please take a look at my actual problem here .. https://stackoverflow.com/questions/46901059/uitableviewcell-subclass-wrong-image-in-cell-or-old-image-bug – lorenzo gonzalez Oct 25 '17 at 18:10
  • You are missing to update the `image` property of the model. I would move the entire `ImageLoader` logic into the model. An asynchronous task in an object which can disappear immediately can / does cause unexpected behavior. The `view` (cell) is supposed only to show the UI data. – vadian Oct 25 '17 at 18:19

1 Answers1

0

You have not returned your cell from cellForRow method so this is the reason why your if statement is returning false. I'm not sure why you are trying to do this but there have to be a better way. If you want to look for visible cells you can use UITableView visibleCells variable.

Michał Kwiecień
  • 2,734
  • 1
  • 20
  • 23
  • thanks for answering, can you please check at my actual problem here ... https://stackoverflow.com/questions/46901059/uitableviewcell-subclass-wrong-image-in-cell-or-old-image-bug – lorenzo gonzalez Oct 25 '17 at 18:09
  • im actually trying to set everything in my uitableviewcell subclass, whats happening is when in a bad connection, the image sets to the wrong cell before updating and setting correctly.. – lorenzo gonzalez Oct 25 '17 at 18:12
  • That's because cells are being reused to save the memory. In your UITableVieCell subclass override method "prepareForReuse" and cancel the download image task. Very common problem ;) – Michał Kwiecień Oct 25 '17 at 18:17