【问题标题】:Image not getting Proper height inside UITableViewCell ios Swift 4图像在 UITableViewCell ios Swift 4 中没有得到适当的高度
【发布时间】:2017-11-02 12:27:19
【问题描述】:

我正在尝试从服务器下载图像并想在我的单元格中加载图像,但是当我在 cellForRowAt 方法中下载时,它不会第一次获得高度。如果我向上滚动并再次向下滚动,图像将具有适当的高度。

使用 Kingfisher 从服务器下载图片

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
        }
        else{
            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
                    }else{
                        print("Image or URL is nil")
                    }
                }
            })
            cell.selectionStyle = .none
            cell.backgroundColor = UIColor.clear
            return cell       
    }
}
}

【问题讨论】:

    标签: uitableview ios11 swift4 uitableviewautomaticdimension xcode9.1


    【解决方案1】:

    下载图像后,您应该重新加载单元格以将其大小更改为合适的大小。滚动时您会获得正确的尺寸,因为 tableView 在需要显示新单元格时会调用 heightForRowAt。所以在DispatchQueue.main.async {里面设置了所有必要的属性UITableView().reloadRows(at: [IndexPath], with: UITableViewRowAnimation.automatic)后重新加载单元格@

    【讨论】:

    • 以及其他部分的 heightForRowAt 高度是多少@Михаил Масло
    • 没有像 tableView.reloadCells(atIndexPaths: [indexPath]) @Михаил Масло 这样的方法
    • @AbhijitHadkar 我更新了答案。关于大小的大小,它将是您已经提供的默认 160,因为您没有加载图像并且不知道大小
    • 它正在重新加载行,但更新速度非常快,以至于我的应用程序崩溃了。
    • @AbhijitHadkar 你能更具体地说明崩溃吗?究竟是什么原因造成的?
    【解决方案2】:

    我使用了这个人的建议:https://stackoverflow.com/a/33499766/8903213

    代码:

    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
             cell.layoutSubviews()
        })
        ...
    

    这似乎对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多