Swift Parse - UITableViewAutomaticDimension从解析下载图像时,它们的大小不正确,直到我滚动

时间:2016-01-20 20:27:34

标签: ios swift uitableview uiview

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

根据图像调整细胞大小。

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

[enter image description here]

图像不加载,直到我开始滚动...如果我向下滚动页面的一半然后回到顶部,我明白了:哪个是正确的

enter image description here

有什么想法吗?我已经在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)
    }

下载图片代码:

    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>()
            }
        }
    }

这是我的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

                        }}}}}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

解决方案1 ​​

将标题和标签添加为内容视图的子视图。这样你就可以免费获得滚动。

scrollView.contentView.addSubView(headerView)
scrollView.contentView.addSubView(tabView)
scrollView.contentView.addSubView(contentView)

let headerHight = 200
let tabHeight = 200
headerView.frame = CGRect(0, 0, UIScreen.mainScreen().bounds.width, headerHight)
tabView.frame = CGRect(0, headerHight, UIScreen.mainScreen().bounds.width, tabHeight)
contentView.frame = CGRect(0, headerHight + tabHeight, UIScreen.mainScreen().bounds.width, contentHeight)

解决方案2

实施UIScrollViewDelegate并在func scrollViewDidScroll(scrollView: UIScrollView)

上移动标题和标签

为标题和制表符的顶部位置设置两个约束和出口,并更改约束的常量值以对应scrollView的位置

解决方案3

tableView中实施所有内容,并将标头设置为tableView.tableHeaderView。如果它应该跟随标题的滚动,那么最好还包括标签栏作为标题的一部分。

答案 1 :(得分:0)

好的,有代码示例(非常难看的UI)

override func viewDidLoad() {
    super.viewDidLoad()

    let screenWidth = view.frame.width
    let screenHeight = view.frame.height
    let subViewHeight = screenHeight / 3

    let mainView = UIScrollView(frame: view.frame)
    mainView.backgroundColor = UIColor.lightTextColor()
    mainView.contentSize = CGSize(width: screenWidth, height: screenHeight + 100)
    view = mainView

    let firstView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: subViewHeight))
    firstView.backgroundColor = UIColor.redColor()

    let secondView = UIView(frame: CGRect(x: 0, y: subViewHeight, width: screenWidth, height: subViewHeight))
    secondView.backgroundColor = UIColor.blueColor()

    let thirdView = UIScrollView(frame: CGRect(x: 0, y: subViewHeight * 2, width: screenWidth, height: subViewHeight + 100))
    thirdView.contentSize = CGSize(width: screenWidth, height: screenHeight)
    thirdView.backgroundColor = UIColor.greenColor()

    view.addSubview(firstView)
    view.addSubview(secondView)
    view.addSubview(thirdView) 
}