【问题标题】:UITableViewAutomaticDimension Cells resize after scroll or pull to refreshUITableViewAutomaticDimension 单元格在滚动或拉动刷新后调整大小
【发布时间】:2016-01-14 16:48:01
【问题描述】:

我添加了一个表格视图,并在单元格中显示图像。我还添加了这段代码:

tableView.estimatedRowHeight = 175
tableView.rowHeight = UITableViewAutomaticDimension

以便单元格根据图像调整大小。

当我启动我的应用程序时,我得到了这个:

直到我开始滚动图像才会加载......如果我向下滚动页面的一半然后返回顶部,我会得到这个(这是正确的):

此外,如果我删除了“赞”按钮并且只将图像单独放在一个单元格中,当我启动我的应用程序时,如果我等待 3 秒而不触摸任何内容,单元格会自行调整大小..?!

有什么想法吗?我在 google 上进行了研究,并为旧版本的 Xcode 尝试了奇怪的解决方案,但似乎没有任何效果!

这是我在 TableViewController 中的其余代码:

extension TimelineViewController: UITableViewDataSource {

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 46
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return timelineComponent.content.count
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerCell = tableView.dequeueReusableCellWithIdentifier("PostHeader") as! PostHeaderTableViewCell

        let post = self.timelineComponent.content[section]
        headerCell.post = post


        return headerCell
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostTableViewCell

        //cell.postImageView?.image = UIImage(named: "Background.png")

        let post = timelineComponent.content[indexPath.section]
        post.downloadImage()
        post.fetchLikes()
        cell.post = post

        cell.layoutIfNeeded()
        return cell
    }
}

extension TimelineViewController: UITableViewDelegate {
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        timelineComponent.targetWillDisplayEntry(indexPath.section)
    }


Download image code:

func downloadImage() {
    // 1
    image.value = Post.imageCache[self.imageFile!.name]

    if image is not downloaded yet, get it
        if (image.value == nil) {

            imageFile?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
                if let data = data {
                    let image = UIImage(data: data, scale: 2.0)!
                    self.image.value = image
                    // 2
                    Post.imageCache[self.imageFile!.name] = image
                }
            }
        }
}

// MARK: PFSubclassing
extension Post: PFSubclassing {
    static func parseClassName() -> String {
        return "Post"
    }

    override class func initialize() {
        var onceToken : dispatch_once_t = 0;
        dispatch_once(&onceToken) {
            // inform Parse about this subclass
            self.registerSubclass()
            // 1
            Post.imageCache = NSCacheSwift<String, UIImage>()
        }
    }
}


And here is my TableViewCell:


var post: Post? {
    didSet {
        postDisposable?.dispose()
        likeDisposable?.dispose()

        if let oldValue = oldValue where oldValue != post {

            oldValue.image.value = nil

        }

        if let post = post {

            postDisposable = post.image
            .bindTo(postImageView.bnd_image)

            likeDisposable = post.likes
            .observe { (value: [PFUser]?) -> () in

                if let value = value {
                    //self.likesLabel.text = self.stringFromUserList(value)
                    self.likeButton.selected = value.contains(PFUser.currentUser()!)
                    // self.likesIconImageView.hidden = (value.count == 0)
                } else {
                    //self.likesLabel.text = ""
                    self.likeButton.selected = false
                    //self.likesIconImageView.hidden = true

                }
            }
        }
    }
}

非常感谢任何帮助!

【问题讨论】:

    标签: ios swift uitableview parsing uitableviewautomaticdimension


    【解决方案1】:

    我猜,你需要在图像最终加载时重新加载单元格,因为当新尺寸的图像到达时,tableView 需要重新计算单元格高度(以及整个 contentHeight)

    post.downloadImage { _ in
        if tableView.indexPathForCell(cell) == indexPath {
            tableView.reloadRowsAtIndexPaths([indexPath], animation: .None)
        }
    }
    

    而downloadImage方法需要调用完成闭包。类似的东西。

    func downloadImage(completion: ((UIImage?) -> Void)?) {
    
        if let imageValue = Post.imageCache[self.imageFile!.name] {
            image.value = imageValue
            completion?(imageValue)
            return
        }
    
        //if image is not downloaded yet, get it
        imageFile?.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
            if let data = data {
                let image = UIImage(data: data, scale: 2.0)!
                self.image.value = image
                // 2
                Post.imageCache[self.imageFile!.name] = image
    
                completion?(image)
            } else {
                completion?(nil)
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答,您介意解释一下吗..我是新手,不太了解它是如何工作的。
    • 调用reloadRowsAtIndexPaths时,tableView会重新计算这些行的高度,因为第一次没有图片,无法计算出实际大小。这就是你需要的。关于 indexPaths:当您滚动 tableView 时,它会一次又一次地重用单元格。而当最终下载完图片时,cell已经可以重复使用5次了,不需要重新加载
    • 我应该把上面添加的代码放到 cellForRowAtIndexPath 中吗?
    • 我已经更新了我的答案,是的,第一个例子是 cellForRowAtIndexPath
    • 嗯,它肯定比以前更好!但是一旦它加载,你可以看到它调整大小并且看起来有点跳跃,不太清楚如何解释它!但是当它加载主页时,您可以看到它全部调整大小,是否有隐藏它或其他什么?如果是这样,我会提出一个新问题..?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多