当我向下滚动并返回时,我的数据在UITableViewCell中发生了变化 [英] My data changes in UITableViewCell when I scroll down and get back

查看:204
本文介绍了当我向下滚动并返回时,我的数据在UITableViewCell中发生了变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在tableView Controller中填充新数据时,我的顶部单元格将被底部的单元格重写. 让我解释.我在底部的最新单元格行中有一张正在异步加载的图片.所有其他单元格均使用应用程序中的图像加载静态图像.当我向下滚动我的应用程序并显示与其他单元格具有不同图像的底部单元格,然后再次向上滚动时,我看到第一个商店图像已更改为底部单元格中加载的动态图像.有人知道为什么会这样吗?

When I'm populating new data in my tableView Controller, my top cell is being rewrite by the bottom one. Let me explain. I have one image that is loading async in the latest cell row, in the bottom. All the other cells are loading static with an image that is in the app. When I scroll down my app and display my bottom cell which have a different image than the others cells, and I scroll up again, I see the first shop image has changed to the dynamic one loaded in the bottom cell. Does anyone know why this is happening?

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

    let cell : shopsTableViewCell = tableView.dequeueReusableCellWithIdentifier("shopCell", forIndexPath: indexPath) as! shopsTableViewCell

    cell.index = indexPath.row
    let qualityOfServiceClass = QOS_CLASS_BACKGROUND
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
    let apiCall = webApi()


        dispatch_async(backgroundQueue, {
            apiCall.downloadPhotoAsync("http://api-ytyg.wbbdev.com/files/shops/\(shops[indexPath.row].id)/\(shops[indexPath.row].featured_img)"){(image: UIImage?) -> Void in
                dispatch_async(dispatch_get_main_queue()){
                    if(image != nil){
                        if (cell.index == indexPath.row){
    //                            shops[indexPath.row].photo = image!
                            cell.shopImg?.image = image
                        }

                    }else{
                        shops[indexPath.row].photo = UIImage.init(named: "No_Image_Available.jpg")!
                    }
                }
            }
        })
    cell.shopName?.text = shops[indexPath.row].name
    cell.shopDescription?.text = shops[indexPath.row].address

    cell.label1?.text = shops[indexPath.row].city + " | " + shops[indexPath.row].distance + "Km"
    cell.becomeFirstResponder()
    return cell
}

推荐答案

这是因为您的单元格正在被重用:

it is because your cells are being reused :

// this line initialize a cell of type shopsTableViewCell 
//if no cell can be reused, get an already used cell else
let cell : shopsTableViewCell 
                 = tableView.dequeueReusableCellWithIdentifier("shopCell",
                                  forIndexPath: indexPath) as! shopsTableViewCell

因此图像和其他数据应该是出队单元格的最后一个图像和数据.

so image and other data should be the last image and data of the dequeued cell.

为避免这种情况,应在单元格类文件中实现prepareForReuse以重置数据和图像. 有关苹果的文档prepareForReuse功能

To avoid this, you shall implement prepareForReuse in the cell class file to reset data and image. docs of apple about prepareForReuse func

来自Apple文档:

如果UITableViewCell对象是可重用的(即,它具有重用标识符),则在从UITableView方法dequeueReusableCellWithIdentifier:返回对象之前,将调用此方法.出于性能原因,您只应重​​置与内容无关的单元格属性,例如Alpha,编辑和选择状态.重新使用单元格时,tableView:cellForRowAtIndexPath:中的表视图委托应始终重置所有内容.如果单元对象没有关联的重用标识符,则不会调用此方法.如果重写此方法,则必须确保调用超类实现.

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

 //Example :
 override func prepareForReuse() {
    super.prepareForReuse()
    //Do reset here
    this.shopImg?.image = nil
    this.shopName?.text = ""
    this.shopDescription?.text = ""

}

这篇关于当我向下滚动并返回时,我的数据在UITableViewCell中发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆