Всякий раз, когда выбрана ячейка из раздела 3. Я обновляю массив DataSource, и цвет фона ячейки изменяется правильно.Проблема повторного использования UITableViewCell. Изменение одной ячейки влияет на других
Однако всякий раз, когда я прокручиваю обратно я начинаю видеть случайные клетки с измененным цветом фона, зная, что я даже не упоминать об этом в моем cellForRowAtIndexPath
методе и каждый раздел в моем tableView
заполняется из отдельного источника данных Array/Dictionary
!
(я использую раскадровку, чтобы обрабатывать все настройки пользовательского интерфейса)
Вот мой код (фокус на раздел 3)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ECTextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettingsTextFieldCellReuseIdentifier
forIndexPath:indexPath];
if (indexPath.section == 0) {
cell.cellTextField.text = [self.personalInfoDataSource valueForKey:kUserValuesKey][indexPath.row];
} else if (indexPath.section == 1) {
cell.cellTextField.text = [self.contactInfoDataSource valueForKey:kUserValuesKey][indexPath.row];
} else if (indexPath.section == 2) {
cell.cellTextField.text = [self.professionalDetailsDataSource valueForKey:kUserValuesKey][indexPath.row];
} else if (indexPath.section == 3) { //---- Problems here
UserMeta *metaObj = self.interestDataSource[indexPath.row];
cell.cellTextField.userInteractionEnabled = NO;
cell.cellTextField.text = metaObj;
if (self.user.INTEREST.count > 0 && [self.user.INTEREST contains:metaObj.name]) {
cell.backgroundColor = [UIColor redColor];
}
}
return cell;
}
А вот где я делаю все Изменения в DataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0 && indexPath.row == 2) {
// Do Stuff
} else if (indexPath.section == 3) { //---- Problems here
ECTextFieldTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
UserMeta *metaObj = self.interestDataSource[indexPath.row];
[self.user.INTEREST addObject:metaObj];
}
}
Хорошая точка. Я закончил тем, что добавил код ячейки «default cell» в «cellForRowAtIndexPath» прямо перед тем, как начать настраивать свои ячейки для каждой «indexPath», и это сделало трюк! Вот сделанные изменения: 'cell.userInteractionEnabled = YES; cell.cellTextField.placeholder = @ ""; cell.cellTextField.text = @ ""; cell.cellAccessoryView.image = nil; ' – Priest