Image not getting Proper height inside UITableViewCell ios Swift 4

uitableview ios11 swift4 uitableviewautomaticdimension xcode9.1

522 观看


80 作者的声誉

I am trying to download image from server and want to load images inside my cell but as i am downloading inside cellForRowAt method it wont get height for the first time. If i scroll up and scroll down again the image will have proper height.

Using Kingfisher to download images from server

var homeList = [NSDictionary]()
var rowHeights : [Int:CGFloat] = [:]

func numberOfSections(in tableView: UITableView) -> Int 
    return 1

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if let height = self.rowHeights[indexPath.row]{
            print(" Height \(height)")
            return height
            return 160

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

    if let homeObject = homeList[safe: indexPath.row] {
        if let dynamicURL =  homeObject["dynamic_card_url"] as? String, dynamicURL != "" {

            tableView.register(UINib(nibName: "DynamicCell", bundle: nil), forCellReuseIdentifier: "\(indexPath.row)")
            let cell = tableView.dequeueReusableCell(withIdentifier: "\(indexPath.row)", for: indexPath) as! DynamicCell

            KingfisherManager.shared.downloader.downloadImage(with: URL(string: dynamicURL)!, options: .none, progressBlock: nil, completionHandler: { (image, error, url, data) in
                DispatchQueue.main.async {

                    if (image != nil || url != nil){
                    let aspectRatio = (image! as UIImage).size.height/(image! as UIImage).size.width
                    cell.dynamicImageView.image = image
                    let imageHeight = self.view.frame.width*aspectRatio
                    self.rowHeights[indexPath.row] = imageHeight
                        print("Image or URL is nil")
            cell.selectionStyle = .none
            cell.backgroundColor = UIColor.clear
            return cell       
作者: Abhijit Hadkar 的来源 发布者: 2017 年 11 月 2 日

回应 (2)


426 作者的声誉

When you downloaded your image you should reload your cell to change it size to appropriate one. You get right sizes as you scrolling because tableView calls heightForRowAt when it needs to display new cell. So inside in DispatchQueue.main.async { reload the cell after setting all necessary properties UITableView().reloadRows(at: [IndexPath], with: UITableViewRowAnimation.automatic)

作者: Mikhail Maslo 发布者: 03.11.2017 07:14


360 作者的声誉

I used this guys suggestion:


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.imageView?.kf.setImage(with: URL(string: urlOfPhoto)!, placeholder: PlaceholderImages.user, completionHandler: {
         (image, error, cacheType, imageUrl) in

and this seems to be working for me.

作者: Michael Economy 发布者: 03.01.2018 05:36