滚动uitableview时重复单元格

时间:2015-07-03 13:55:41

标签: ios objective-c swift uitableview

我正在开发新闻Feed,我正在使用uitableview来显示数据。我在其他线程中同步加载每个单元格数据并使用协议方法显示加载的数据:

func nodeLoaded(node: NSMutableDictionary) {
    for var i = 0; i < nodesArray.count; ++i {
        if ((nodesArray[i]["id"] as! Int) == (node["id"] as! Int)) {
            nodesArray[i] = node
        }
    }
}

问题是当我滚动我的uitableview(同时加载数据时),我的一些单元重复(8行具有相同的内容,如第一行,或6行具有相同的内容,如第二行)。当我滚动一段时间后(我想在加载数据后)然后一切都变得正常了。 我在寻找答案,发现我必须在cellForRowAtIndexPath检查单元格是否为nill,但在swift中我的代码与目标C中的代码不同:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: NewsCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewsCell
    var node = nodesArray[indexPath.row] as! NSDictionary
    if (node["needLoad"] as! Bool) {
        dbHelper.getNode(node["id"] as! Int, hash: node["id"] as! Int, tableName: DbHelper.newsTableName, callback: self)
    } else {
        cell.id = node["id"] as! Int
        cell.titleLabel.text =  node["title"] as? String
        cell.descriptionLabel.text = node["description"] as? String
        cell.imgView.image = WorkWithImage.loadImageFromSD((node["image"] as! String))
    }
    return cell
}

此外,我无法检查cell == nil bcs的二进制错误(NewsCell是否为零)。

我该怎么办? THX。

1 个答案:

答案 0 :(得分:5)

您似乎为UITableViewCell创建了一个单独的类。您的代码存在的问题是,重复发生时您没有重置标签。

自定义UITableviewCell类中的Oveeride prepareForReuse方法,并在那里重置您的接口。这应该可以解决问题。

相关问题