2016-01-20 62 views
1

对于我的问题可能没有很好的解决方案,但我想问一下以防万一。图像和scrollView渲染使我的桌面视图不连贯

我有一个tableview,其中每个单元格包含可变宽度的水平滚动视图。每个单元格的滚动视图的宽度取决于该单元格的图像的数量和大小。一切工作都很好,但tableview的滚动不如以前那么流畅。我使用Parse来检索图像(和pfquertableviewcontroller),但这个问题应该适用于一般的tableviews。

这里是我的tableview代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? { 

    if var cell:MainFeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as? MainFeedTableViewCell{ 
    if(cell == nil) { 
     cell = NSBundle.mainBundle().loadNibNamed("MainFeedTableViewCell", owner: self, options: nil)[0] as? MainFeedTableViewCell 
    } 

    cell?.parseObject = object 
    return cell 
    } 
} 

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    if let c = (cell as? MainFeedTableViewCell){ 
     c.setUpObject()//this is where images are set 
    } 
} 


override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    if let c = (cell as? MainFeedTableViewCell){ 
     c.resetObject() //this sets most of the properties to nil 
    } 
} 

这里是我的细胞功能,其中图像设置

func setUpObject(){ 

    //I left out several lines of code where label text is set from the parseObject that is set when the cell is created 

    //setting images **problems here** 

    if let numImages = self.parseObject?["numImages"] as? Int{ 
     self.newWidth1 = self.parseObject?["width1"] as? CGFloat 
     self.newWidth2 = self.parseObject?["width2"] as? CGFloat 
     self.newWidth3 = self.parseObject?["width3"] as? CGFloat 
     self.newWidth4 = self.parseObject?["width4"] as? CGFloat 
     self.newWidth5 = self.parseObject?["width5"] as? CGFloat 
     if numImages == 1{ 
      self.containerView = UIView(frame: CGRect(x: self.scrollView.frame.minX, y: self.scrollView.bounds.minY - 90, width: self.scrollView.frame.width, height: self.scrollView.frame.height)) 
      self.image1 = PFImageView(frame: CGRect(x: self.scrollView.frame.minX , y: self.scrollView.frame.minY, width: self.scrollView.frame.width, height: self.scrollView.frame.height)) 
      self.image1?.contentMode = UIViewContentMode.ScaleAspectFit 

      self.image1!.file = self.parseObject?["image"] as? PFFile 
      self.image1!.loadInBackground() 
      self.containerView!.addSubview(self.image1!) 

      self.scrollView.contentSize = self.containerView!.bounds.size 
      self.scrollView.addSubview(self.containerView!) 
     } 
     else if numImages == 2{ 
      if self.newWidth1 + self.newWidth2 < self.scrollView.frame.width{ 
       self.containerView = UIView(frame: CGRect(x: self.scrollView.frame.midX - (self.newWidth1 + self.newWidth2)/2, y: self.scrollView.bounds.minY - 90, width: (self.newWidth1 + self.newWidth2), height: self.scrollView.frame.height)) 
      } 
      else{ 
       self.containerView = UIView(frame: CGRect(x: self.scrollView.frame.minX, y: self.scrollView.bounds.minY - 90, width: (self.newWidth1 + self.newWidth2), height: self.scrollView.frame.height)) 
      } 
      self.image1 = PFImageView(frame: CGRect(x: self.scrollView.frame.minX , y: self.scrollView.frame.minY, width: self.newWidth1, height: self.scrollView.frame.height)) 

      self.image1!.file = self.parseObject?["image"] as? PFFile 
      self.image1!.loadInBackground() 
      self.containerView!.addSubview(self.image1!) 
      self.image2 = PFImageView(frame: CGRect(x: (self.scrollView.frame.minX + self.newWidth1), y: self.scrollView.frame.minY, width: self.newWidth2, height: self.scrollView.frame.height)) 

      self.image2!.file = self.parseObject?["image2"] as? PFFile 
      self.image2!.loadInBackground() 
      self.containerView!.addSubview(self.image2!) 
      self.subLayer = CALayer() 
      self.subLayer.backgroundColor = UIColor.whiteColor().CGColor 
      self.subLayer.frame = CGRect(x: self.newWidth1, y: self.scrollView.frame.minY, width: 1, height: self.scrollView.frame.height) 
      self.containerView!.layer.addSublayer(self.subLayer) 
      self.scrollView.contentSize = self.containerView!.bounds.size 
      self.scrollView.addSubview(self.containerView!) 
     } 
     //repeat similar code for cases where there are 3, 4, or 5 images 

有可能与动态调整滚动视图大小的根本问题并及时将其添加到超视图中,但我试图按照设计师给我的设计模型。

下面是对细胞滚动视图看起来像(在用细白线分开滚动视图的每个图像)

enter image description here

回答

0

删除您willDisplayCell和didEndDisplayingCell。当你滚动并且你的setUpObject代码会触发,虽然不是很大,但会稍微阻塞主线程。在返回cellForRowAtIndexPath中的单元格之前,请将setUpObject()移至右侧。

根据您有多少行和性能要求,您还可以调整viewController以提前下载所有图像,并将它们传递给单元格,而不是将它们加载到单元格内。

+0

对不起,我没有早点接受 - 我正在做其他事情几天。无论如何,我的setUpObject()调用先前在cellForRowAtIndexPath中,但我根据别人的建议进行了更改。它把事情平滑一些以便将其切换回来,但它还远没有完美。有太多的行可以预加载所有的图像,但是我可以一次做10-15行。 – LulzCow