0

I am using dynamic height for tableview cells and Haneke for downloading and caching image using the following code:

override func viewDidLoad() {
    super.viewDidLoad()
    myTableView.rowHeight = UITableViewAutomaticDimension
    myTableView.estimatedRowHeight = 500
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:ImageCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageCellTableViewCell;

    cell.myImageView.hnk_setImage(from: URL(string: imageUrl), placeholder: nil)
    return cell
}

Here I need to resize the cell height when setting image after downloading. How can I resize the tableview cell after downloading the image.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
UIBittu
  • 215
  • 2
  • 13

3 Answers3

2

Just call

tableView.beginUpdates() tableView.endUpdates()

after image is downloaded and height constraint is set. Hope this helps!

EDIT:

Another option, to give the right height to the cell, would be receiving the image size from the server. This way you could feed table with just right calculated cell height.

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
1

just add the proper constraint to your image, for example , top and left constraint with, aspect ratio constraint.(you can do it even in your storyboard interface)

Hamish
  • 1,685
  • 22
  • 37
  • I already completed those.My question is, for the first time the cell height will be according to the constraints (before downloading),the next time when you scroll to the same row the height will be according to the image height(after downloading).I want the second height to be reflected in the cell automatically. – UIBittu Oct 03 '17 at 09:14
-2

Set table view's row height like:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:ImageCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageCellTableViewCell;

    cell.myImageView.hnk_setImage(from: URL(string: imageUrl), placeholder: nil)
    tableView.rowHeight = cell.myImageView.frame.origin.y + cell.myImageView.frame.size.height + (constant what every you want to give like: - 20/30 as per your need)

    return cell
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Raj Oriya
  • 78
  • 4
  • 2
    No, do not do this. The table view's `rowHeight` property should only be set once, not every time a cell is requested. – rmaddy Oct 03 '17 at 14:03
  • but i am using it in my app and it is working fine that is why i have answered – Raj Oriya Oct 04 '17 at 04:24