UITableViewAutomaticDimension单元格在滚动或拉动刷新后调整大小

时间:2016-01-14 16:48:02

标签: ios swift uitableview parsing uitableviewautomaticdimension

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

tableView.estimatedRowHeight = 175
tableView.rowHeight = UITableViewAutomaticDimension

根据图像调整细胞大小。

当我启动我的应用程序时,我明白了:

enter image description here

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

enter image description here

此外,如果我删除了like按钮并且只在单元格中放置了图像,当我启动我的应用程序时,如果我等待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

                }
            }
        }
    }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

我想,你需要在最终加载图像时重新加载单元格,因为当新图像的图像到达时,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)
        }
    }
}