简体   繁体   English

滚动后,表格视图单元格混乱

[英]Table view cells mess up after scrolling

I've got a UITableViewCell, containing a specific of UIImages that I generate from code. 我有一个UITableViewCell,其中包含我从代码生成的特定UIImages。

I've got this problem: the images are generated fine when I first load the view. 我遇到了这个问题:当我第一次加载视图时,图像生成良好。 However, if, for example, I scroll down and then I go back up, I find a completely different set of images. 但是,例如,如果我向下滚动然后又向上滚动,则会发现一组完全不同的图像。

I imagine this is a problem connected to the reusable cells, but I really don't know how to solve it. 我想这是与可重用单元有关的问题,但我真的不知道如何解决。

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

Here's the code for cellForRowAtIndexpath: 这是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

First of all, you are dispatching to the main thread twice, and once in an existing dispatch call. 首先,您要分派两次到主线程,一次是在现有分派调用中。 You should not need to dispatch anything to the main thread in cellForRowAtIndexPath, unless you are doing background work you need to return, like a network fetch. 除非您正在做后台工作,就需要像网络获取那样返回,否则您无需在cellForRowAtIndexPath中将任何内容分派到主线程。

Second, you should set your image on the reusable cell to nil before the cell is loaded. 其次,在加载单元之前,应将可重用单元上的图像设置为nil。 image.image = nil right after you dequeue the cell. image.image = nil出队后立即。 That might help make sure you are not reusing the image. 这可能有助于确保您没有重用该图像。

Apple and others have some very great tutorials on how to efficiently load images into a table view, also. 苹果公司和其他公司也有一些非常好的教程,介绍了如何有效地将图像加载到表格视图中。

Using tag along with reusable cells is a bad idea. 与可重复使用的单元格一起使用标记是个坏主意。 Try to code the same without the help of tag. 尝试在没有标签帮助的情况下进行相同的编码。 Giving indexpath.row as tag value cause weired issues. 将indexpath.row用作标记值会引起奇怪的问题。 I think this will help 我认为这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM