2016-01-20 82 views
-1

我已经添加了一个表视图,我在单元格中显示图像。我还添加了以下代码:Swift解析 - UITableViewAutomaticDimension从解析下载图像时,他们是不正确的大小,直到我滚动

使单元根据图像调整大小。

当我虽然推出我的应用程序,我得到这个:

[enter image description here]

而且图像不会加载,直到我开始滚动...如果我向下滚动页面的一半再回去顶,我得到这样的:这是正确的

enter image description here

任何想法?我研究过谷歌,并尝试了老版本的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 

         }}}}} 

任何帮助真的不胜感激!

+0

你必须把所有三个视图放到一个大scrollViews :) – Breek

+0

@Breek嗯,我试着用UIScrollView,但它似乎没有工作..更可能是我的错!你介意给我一个小例子或下面的答案吗?!不管怎么说,多谢拉! – Charles

+0

昨天你问了[相同的问题](http://stackoverflow.com/questions/34864981/how-to-make-full-viewcontroller-scroll/34867916#34867916),然后在3个小时前删除了它。当你可能再次删除它时,为什么有人会回答这个问题? – Caleb

回答

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。无论如何,将标签栏作为标题的一部分,如果它应该遵循标题的滚动,可能会更好。

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