简体   繁体   English

Tableview 重用单元格并首先显示错误的数据

[英]Tableview reusing the cells and showing wrong data first

Hello I've been having this problem for awhile.您好,我遇到这个问题有一段时间了。 I want to stop the tableview from reusing the cell.我想阻止 tableview 重用单元格。 It keeps displaying the wrong information when i scroll then it shows the right thing like a few milliseconds.当我滚动时它一直显示错误的信息,然后它会显示正确的信息,比如几毫秒。 How can i stop the tableview from reusing the cell or how can i reuse the cell and make it not do that.如何阻止 tableview 重复使用单元格,或者如何重复使用单元格并使其不这样做。

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cats.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "CategoryTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CategoryTableViewCell
    cell.nameLabel.text = cats[indexPath.row].categoryName
    cell.subNameLabel.text = cats[indexPath.row].appShortDesc
    let catImageUrl = cats[indexPath.row].imageUrl
            let url = NSURL(string: "https:\(catImageUrl)")
            let urlRequest = NSURLRequest(URL: url!)
            NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in
                if error != nil {
                    print(error)
                } else {
                    if let ass = UIImage(data: data!) {
                            cell.photoImageView.image = ass
                        }
                    self.loading.stopAnimating()
                }
            }
    return cell
}

The problem is that you are seeing an image from a previous cell. 问题是您正在看到前一个单元格中的图像。 Simply initialize the image to nil when you dequeue the reused cell: 当您将重用的单元格出列时,只需将图像初始化为nil

cell.photoImageView.image = nil

or set it to a default image of your choosing. 或将其设置为您选择的默认图像。


Note, the way you are updating the image after it loads has issues. 请注意,加载后更新图像的方式存在问题。

  1. The row may no longer be on screen when the image finally loads, so you will be updating a cell that has been reused itself. 当图像最终加载时,该行可能不再在屏幕上,因此您将更新已经重用的单元格。

  2. The update should be done on the main thread. 更新应该在主线程上完成。

A better way to do this would be to have an array that caches the images for the cells. 更好的方法是使用一个缓存单元格图像的数组。 Load the image into the array, and then tell the tableView to reload that row. 将图像加载到数组中,然后告诉tableView重新加载该行。

Something like this: 像这样的东西:

dispatch_async(dispatch_get_main_queue()) {
    self.imageCache[row] = ass
    self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 0)],
        withRowAnimation: .None)
}

override prepareForReuse() method in your cell class and reset your values覆盖单元类中的 prepareForReuse() 方法并重置您的值

 override func prepareForReuse() {
        super.prepareForReuse()
        nameLabel.text = ""
    }

This method is called every time the UITableView before reuses this cell每次 UITableView 重用此单元格之前都会调用此方法

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

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