【问题标题】:Image and scrollView rendering making my tableview choppy图像和滚动视图渲染使我的表格视图断断续续
【发布时间】:2016-01-20 16:37:07
【问题描述】:

我的问题可能没有好的解决方案,但我想问一下以防万一。

我有一个表格视图,其中每个单元格都包含一个可变宽度的水平滚动视图。每个单元格的滚动视图的宽度取决于该单元格图像的数量和大小。一切都运行良好,但 tableview 滚动不如它可以顺利。我正在使用 Parse 来检索图像(和 pfquertableviewcontroller),但是这个问题通常应该适用于 tableviews。

这是我的表格视图代码:

 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

动态调整滚动视图的大小并将其及时添加到超级视图可能存在一个基本问题,但我正在尝试遵循我的设计师给我的设计模型。

这是单元格上的滚动视图的样子(滚动视图中的每个图像由一条细白线分隔)

【问题讨论】:

    标签: ios swift uitableview parse-platform uiscrollview


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我没有早点接受——我这几天一直在做别的事情。无论如何,我的 setUpObject() 调用以前在 cellForRowAtIndexPath 中,但我根据别人的建议更改了它。它确实使事情变得平滑了一点以将其切换回来,但它仍然远非完美。预加载所有图像的行太多,但也许我可以一次加载 10-15 行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多