TableView生成相同标签的多个副本,swift

时间:2015-11-27 14:41:54

标签: ios swift uitableview uitabbarcontroller tableviewcell

出于某种原因,我的手机标题不断重复。这是代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactsTableViewCell

    // Configure the cell...
    cell.contactNumberLabel.text = allNumbers[indexPath.row]

    // App downloads confirmedContactList from server
    // Before data is downloaded confirmedContactList is empty and 'for in' loop crashes app.
    if confirmedContactList != new {

        // Title should appear only in cells where arrays allNumbers and confirmedContactList are identical
        for n in 0..<self.allNumbers.count {
            for k in 0..<self.confirmedContactList.count {
                if self.allNumbers[n] == self.confirmedContactList[k]{
                    cell.nameLabel.text = "This Title Keeps Duplicating"
                }
            }
        }
    }
    return cell
}

基本上,有两个包含电话号码的阵列,如果confirmedContactList中存在单元格中显示的联系号码,那么cell.nameLabel应该说明一些特定内容。

1 个答案:

答案 0 :(得分:0)

为已经显示的indexPaths调用cellForRowAtIndexPath方法时,nameLabel文本未设置为默认值。

如果先前使用的值通过了confirmContactList测试,那么即使新值未通过测试,您也会看到标题。

只需添加一行就可以了:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactsTableViewCell

    // Configure the cell...
    cell.contactNumberLabel.text = allNumbers[indexPath.row]

    // reset the label
    cell.nameLabel.text = ""

    // App downloads confirmedContactList from server
    // Before data is downloaded confirmedContactList is empty and 'for in' loop crashes app.
    if confirmedContactList != new {

        // Title should appear only in cells where arrays allNumbers and     confirmedContactList are identical
        for n in 0..<self.allNumbers.count {
            for k in 0..<self.confirmedContactList.count {
                if self.allNumbers[n] == self.confirmedContactList[k]{
                    cell.nameLabel.text = "This Title Keeps Duplicating"
                }
            }
        }
   }
    return cell
}