Sergey Krasiuk
Sergey Krasiuk

Reputation: 198

Reusable TableViewCell asynchronous height

I have custom UITableViewCell:

enter image description here

Also have some objects (get from server) with imageURL property.

Task: Need asynchronous download images (via Kingfisher). If image not exist - hide ImageView. Some works fine, BUT:

P.S. Tried use tables updates, reload row, also use different cell for empty cell - not successfully. Also tried find solution in SO, but funded only theory :)

Have ideas (not only be theory)?

Thanks

P.S.S. Hardcoded cell sizes based on images (have or not). And also change constraint imageView (0 or xxx). If understand right - tableView don't know cells sizes (because was set UITableViewAutomaticDimension)

Not GOOD solution, but works.

enter image description here

Upvotes: 2

Views: 441

Answers (1)

Fangming
Fangming

Reputation: 25261

For your first problem, there are two ways to solve it. You can either have a super fast server, or preload image height in your arrayOfObjects. That is, when you get your list of objects, you also check if there is an image. So when you load the cell, you don't need to wait for the download and then show/hide the image. Doing this not only make the scroll better, but also reduce calls to the image server.

For your second problem, have a flag in your object from the array indicating if the object has an image or not. And if it does not have image, stop downloading again from the server. A sample code will look like this:

//itemObject now has property called loadFlag which set to true by default

if itemObject.loadFlag {
  //Your loading image code . If image is nil, set this flag to false
} else { //This will prevent reused cell having image by default
  cell.pictureView.isHidden = true
  cell.picture = nil
}

Upvotes: 0

Related Questions