2015-11-27 2 views
0

По какой-то причине названия моих ячеек дублируют. Вот код:TableView делает несколько копий одной и той же метки, swift

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 должен сказать что-то конкретное.

ответ

0

Когда метод cellForRowAtIndexPath вызывается для уже отображаемых индексных таблиц, текст nameLabel не установлен в значение по умолчанию.

Если ранее использовавшееся значение передало подтвержденный тестContactList, вы увидите заголовок, даже если новое значение не пройдет тест.

Просто добавьте строку, и вы будете в порядке:

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 
} 
Смежные вопросы