【问题标题】:UIImageViews are repeating in cellUIImageViews 在单元格中重复
【发布时间】:2015-09-07 16:38:38
【问题描述】:

我有一个包含 UIScrollView 的单元格,其中包含一些 UIImageViews 将保存从 Parse 下载的图像。

当它在我的 tableview 中加载前 5 个时效果很好,但在第五个之后,越来越多的 UIImageViews 堆叠在一起。

我相信问题出在我使用的时候:

cell.scrollView.addSubview(myImageView)

这会在 scrollView 中添加并不断添加新的 UIImageView。

我一直在尝试不同的方法来删除 UIImageViews,但我似乎无法正确实现它。

我又回到了原点,但现在我知道是什么导致了问题。

这就是我从 Parse 下载图像的方式:

photoQuery.findObjectsInBackgroundWithBlock {
    (objects:[AnyObject]?, error: NSError?) -> Void in

    if error == nil {
        for object in objects!{

            var arrayFile = [PFFile]() // hold both image into temp array

            if object.objectForKey("imageOne") != nil {
                self.resultsHasImageOneFile.append(object.objectForKey("imageOne") as! PFFile)
                arrayFile.append(object.objectForKey("imageOne") as! PFFile)
            }

            if object.objectForKey("imageTwo") != nil {
                self.resultsHasImageTwoFile.append(object.objectForKey("imageTwo") as! PFFile)

                arrayFile.append(object.objectForKey("imageTwo") as! PFFile)
            }
            // will add imageThree
            // will add imageFour
            // will add imageFive

            self.masterImageArray.append(arrayFile)//add temp array into Master Image array
            self.resultsTable.reloadData()
        }
    }
}

这就是我将 UIImageViews 添加到我的滚动视图中的方式:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // configure cell....

    var imageArray = masterArray[indexPath.row]

    cell.imageScrollView.tag = indexPath.row // do something with this?????

    for var i = 0; i < imageArray.count; i++ {

        var myImageView: UIImageView = UIImageView()

        myImageView.frame.size.width = imageWidth
        myImageView.frame.size.height = imageHeight
        myImageView.frame.origin.x = xPosition

        imageArray[i].getDataInBackgroundWithBlock{
            (imageData: NSData?, error: NSError?) -> Void in

            myImageView.image = UIImage(data: imageData!)
        }

        cell.imageScrollView.addSubview(myImageView)

        xPosition = imageWidth
        scrollViewContentSize += imageWidth

        cell.imageScrollView.contentSize = CGSize(width: scrollViewContentSize, height: imageHeight)

        cell.imageScrollView.tag = indexPath.row // do something with this?????
    }
}

我尝试使用以下内容删除单元格内容中的内容,但它说它找到了 nil,这会崩溃。

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    var cell = FriendsFeedTableViewCell()
    cell.imageScrollView.removeFromSuperview()
}

在我的 FriendsFeedTableViewCell 类中,我只有一个标签和一个名为

的 UIScrollView
@IBOutlet weak var imageScrollView: UIScrollView!

如何正确删除已经创建的 UIImageViews?

【问题讨论】:

  • 您是否尝试从解析中下载图像然后将其显示到表格视图中
  • 它们已经下载。我将 NSData 保存在主阵列中。在 IndexPath,它为他们所做的 NSData 创建 UIImageViews。因为我有透明度(使用 PNG),所以我可以在其他单元格中看到其他 UIImageView。它们是在彼此之上一遍又一遍地创建的。
  • 你能贴一张那个tableview的图片吗
  • 为什么要在 didEndDisplayingCell 方法中初始化一个新单元格?你能展示你的完整代码以便我们找到问题吗?
  • 并显示 FriendsFeedTableViewCell 类

标签: ios swift uitableview uiscrollview uiimageview


【解决方案1】:

我认为您通过访问 Parse Twice 做了太多工作,您应该下载每张图片一次并将它们插入到单个对象数组中。示例:

    var photoQuery = PFQuery(className: "")
photoQuery.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in

    if error == nil
    {
        if let objects = objects as? [PFObject]
        {
            for singleObject in objects
            {
                var ImageDataToDownload = singleObject["images"] as! PFFile
                ImageDataToDownload.getDataInBackgroundWithBlock({ (dataToget:NSData?, error:NSError?) -> Void in
                    if error == nil
                    {
                        if let  image = UIImage(data: dataToget!) {
                                // pass this image to  your array 
                                // reload tableview
                        }
                    }
                })
             }}}}

然后在 cellForRowAtIndexPath()

    cell.imageView.image = arrayFromQuery[indexPath.row]
  then you take care of your scrollview inside that cell

【讨论】:

  • 你说的太对了。这完全有道理。我认为它会快一点,因为它只保存 NSData 而不是图像,但我只是让自己很困惑。到目前为止,我将重写我的应用程序的一半。我会在 2-3 天后回来。
  • 是的,没问题,如果有什么问题请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 2015-01-22
相关资源
最近更新 更多