2

I have a model

Where in ImageViewURL I get a photo and save it in ImageView, and the code for downloading

import Foundation

import UIKit

class Modal:NSObject {

var name:String?
var date:String?
var ImageViewURL = ""
var ImageView:UIImage?
var stmp: Int?
var id: String?
var text:String?

}

When you click on a cell, the placeholderImage is transferred, after I go back down, go up and click on the same cell the photo is changing

Let u = URL (string: "link" + (modals [indexPath.row] .ImageViewURL))

cell.ImageView.sd_setImage (with: u, placeholderImage: UIImage (named: "loading"))

self.modals [indexPath.row] .ImageView = cell.ImageView.image!

The question is how to run the

self.modals method [indexPath.row] .ImageView = cell.ImageView.image! Before loading placeholderImage

all code

var modals = [Modal]()
override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  modals.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if modals.count == 0 {
        return UITableViewCell()
    }
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    cell.date?.text = modals[indexPath.row].date
    cell.name?.text = modals[indexPath.row].name

   let u = URL(string: "http://www.---.com" + (modals[indexPath.row].ImageViewURL))
    cell.ImageView.sd_setImage(with: u, placeholderImage: UIImage(named: "loading"))
    self.modals[indexPath.row].ImageView = cell.ImageView.image!


    return cell

}



 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    self.navigationController?.navigationBar.topItem?.title = ""

    let vc  = segue.destination as? ViewControllerDetail

    vc?.detail = modals[(tableView.indexPathForSelectedRow?.row)!]
}

image1 image 2

programmer
  • 99
  • 8

2 Answers2

1

Is the cell image being updated in cell for row at index path?

Table views are designed to reuse cells. After the cell leaves the screen, it is reused as a cell entering the screen. The cell for row at index path method gets called every time a cell is needed. If you don't update the image in cell for row at index path, you may still see the "old" image.

Taylor M
  • 1,855
  • 1
  • 14
  • 20
0

So, it looks like this line:

  self.modals[indexPath.row].ImageView = cell.ImageView.image!

is probably the culprit.

If you pass the Modal instance and use its ImageView property to populate your detail view's image view, you may be getting the current image on the cell before it's been loaded from the web.

Taylor M
  • 1,855
  • 1
  • 14
  • 20
  • Without this code, `self.modals [indexPath.row] .ImageView = cell.ImageView.image!` The application crashes when you click on a cell "fatal error: unexpectedly found nil while unwrapping an Optional value" – programmer Mar 20 '17 at 05:18