滚动uitableview时重复单元格

Pan*_*xim 1 objective-c uitableview ios swift

我正在开发新闻源,我正在使用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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我滚动我的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
}
Run Code Online (Sandbox Code Playgroud)

另外我无法检查cell == nil bcs的二进制错误(NewsCell不能为零).

我该怎么办?谢谢.

San*_*eep 5

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

在自定义UITableviewCell类中使用Oveeride prepareForReuse方法并在那里重置接口.这应该解决问题.