ios - 滚动后表格 View 单元格困惑

标签 ios swift uitableview

我有一个 UITableViewCell,其中包含我从代码生成的特定 UIImage。

我遇到了这个问题:当我第一次加载 View 时,图像生成得很好。但是,例如,如果我向下滚动然后返回,我会发现一组完全不同的图像。

我想这是与可重复使用的单元有关的问题,但我真的不知道如何解决它。

for collabsImages in secImageUrls[indexPath.row] {

    dispatch_async(dispatch_get_main_queue()){
        let image = UIImageView()
        image.tag = i
        image.userInteractionEnabled = true
        image.frame = CGRectMake(CGFloat(10 + i*50 + 7*i), self.view.frame.height/2 - self.navigationController!.navigationBar.frame.height - 55, 40, 40)
        image.layer.cornerRadius = image.frame.width/2
        image.layer.masksToBounds = true
        image.sd_setImageWithURL(NSURL(string: collabsImages), placeholderImage: nil, options: [])
        cell1.addSubview(image)
        let didTapOnCollabImage = CollabsGestureRecognizer(target: self, action: "didTapOnCollabImage:", row: indexPath.row)
        image.addGestureRecognizer(didTapOnCollabImage)

        i++
    }
}

这是 cellForRowAtIndexpath 的代码:

let cell1 : cellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! cellTableViewCell


        tableView.allowsSelection = false
        cell1.profileImg.userInteractionEnabled = true

        let tappedOnImage = UITapGestureRecognizer(target: self, action: "tappedOnImage:")

        cell1.profileImg.tag = indexPath.row
        cell1.profileImg.addGestureRecognizer(tappedOnImage)


dispatch_async(dispatch_get_main_queue()) {

                cell1.profileImg.center = CGPointMake(35, 35)
                cell1.usernameLbl.frame = CGRectMake(cell1.profileImg.frame.maxX + 7, cell1.profileImg.frame.minY, self.view.frame.size.width/2, 20)
                cell1.usernameLbl.text = (self.jsonFeeds[indexPath.row]["author"] as? String)!
                cell1.titleLbl.text = (self.jsonFeeds[indexPath.row]["title"] as? String)
                cell1.titleLbl.frame = CGRectMake(cell1.profileImg.frame.maxX + 7, cell1.usernameLbl.frame.maxY + 7, self.view.frame.size.width-20, 20)

                let textLabel = UILabel(frame: CGRectMake(cell1.profileImg.frame.minX, cell1.titleLbl.frame.maxY + 20, self.view.frame.width-30, 90))
                //textLabel.center = CGPointMake(cell1.center.x, textLabel.center.y)
                textLabel.textAlignment = NSTextAlignment.Left
                textLabel.numberOfLines = 7
                textLabel.text = self.jsonFeeds[indexPath.row]["text"] as? String
                textLabel.backgroundColor = UIColor(red: 1/255, green: 109/255, blue: 127/255, alpha: 0.1)
                cell1.contentsView.addSubview(textLabel)

                let btn = UIButton()
                btn.frame = CGRectMake(cell1.frame.maxX - 150, textLabel.frame.maxY + 30, 130, 24)
                btn.layer.cornerRadius = 15
                btn.layer.borderWidth = 1
                btn.setTitle("MESSAGE", forState: UIControlState.Normal)
                btn.titleLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 14)
                btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
                btn.layer.borderColor = UIColor.blackColor().CGColor

                cell1.contentsView.addSubview(btn)






            let block: SDWebImageCompletionBlock! = {

                (image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) -> Void in

            }


            cell1.profileImg.sd_setImageWithURL(NSURL(string: self.imageUrls[indexPath.row]!), completed: block)

            }

            var i = 0
            for collabsImages in secImageUrls[indexPath.row] {

                if collabsImages != "This is a post"{



                    dispatch_async(dispatch_get_main_queue()){
                        let image = UIImageView()
                        image.tag = i
                        image.userInteractionEnabled = true
                        image.frame = CGRectMake(CGFloat(10 + i*50 + 7*i), self.view.frame.height/2 - self.navigationController!.navigationBar.frame.height - 55, 40, 40)
                        image.layer.cornerRadius = image.frame.width/2
                        image.layer.masksToBounds = true
                        image.sd_setImageWithURL(NSURL(string: collabsImages), placeholderImage: nil, options: [])
                        cell1.contentsView.addSubview(image)
                        let didTapOnCollabImage = CollabsGestureRecognizer(target: self, action: "didTapOnCollabImage:", row: indexPath.row)
                        image.addGestureRecognizer(didTapOnCollabImage)

                        i++
                    }
                }
            }
        }


        return cell1

最佳答案

首先,您将两次分派(dispatch)到主线程,一次在现有的分派(dispatch)调用中。您不需要将任何内容分派(dispatch)到 cellForRowAtIndexPath 中的主线程,除非您正在执行需要返回的后台工作,例如网络获取。

其次,您应该在加载单元格之前将可重用单元格上的图像设置为零。在使单元出列后立即 image.image = nil 。这可能有助于确保您不会重复使用该图像。

Apple 和其他公司也有一些非常好的教程,介绍如何有效地将图像加载到表格 View 中。

关于ios - 滚动后表格 View 单元格困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373256/

相关文章:

ios - PerformSegue 从 TabBarController 到 ViewController 而不隐藏 TabBar

ios - 如何复制 ios 版本的 iPhone?

ios - 从带有纹理的 URL 将 .scn 文件导入场景

iphone - 自定义 UITableview 单元格辅助功能无法正常工作

ios - 尝试上传到 Apple App Store 时出现 -23690 代码?

iphone - 文件句柄选项未出现

swift - XCode 服务器 : Opening import file for module 'MobileCoreServices' : Permission denied

Swift 编译器错误,同时通过 getter 函数提供属性来遵守协议(protocol)

ios - Swift 2 核心数据 : delete object ok! 但 TableView 中没有刷新行

ios - Swift iOS,UITableView 无