У меня есть табличное представление, которое отображает ячейки. Некоторые из этих ячеек должны быть удалены с помощью кнопки переключения, а затем некоторые добавлены.Xcode, добавляющий новую ячейку TableView, добавляет новую ячейку, но показывает текстовое поле удаленной ячейки
Итак, у меня есть 4 ячейки в моей третьей секции. Их названия в порядке возрастания следующие: то же самое, начало, конец, продолжительность. Вторая и третья ячейки в моем списке имеют текстовые поля как accesoryViews
, и они оба удаляются, когда кнопка переключения (в верхней ячейке) отключена. Итак, это оставляет меня с двумя ячейками: одинаково? и продолжительность в моем третьем разделе.
Если я нажму кнопку, он добавит в 2 ячейки в тот же раздел (раздел 3), но по какой-то причине они имеют текстовые поля из 2 ячеек, которые были удалены в качестве их аксессуаров.
Мой код выглядит следующим образом:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"creatures"];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"creatures"];
}
switch (indexPath.section)
{
case 0:
break;
case 1:
break;
case 2:
cell.textLabel.text = [timesTitles objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
sameTimeEverydaySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
[sameTimeEverydaySwitch setOn:YES];
[sameTimeEverydaySwitch addTarget:self action:@selector(sameEverydaySwitchChanged) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = sameTimeEverydaySwitch;
}
if ([[timesTitles objectAtIndex:indexPath.row] isEqualToString:@"Start time:"]) {
cell.accessoryView = startTimeTextfield;
NSLog(@"log");
}
if ([[timesTitles objectAtIndex:indexPath.row] isEqualToString:@"End time:"]) {
cell.accessoryView = endTimeTextfield;
NSLog(@"log2");
}
if ([[timesTitles objectAtIndex:indexPath.row] isEqualToString:@"Meeting duration"]) {
cell.accessoryView = meetingDurationTextfield;
}
break;
default:
cell.textLabel.text = @"Not Found";
}
return cell;
}
Когда кнопка переключена, звоню:
if (![sameTimeEverydaySwitch isOn]) {
for (int j = 0; j < timesTitles.count; j++) {
if ([[timesTitles objectAtIndex:j] isEqualToString:@"Start time:"]) {
[timesTitles removeObjectAtIndex:j];
}
if ([[timesTitles objectAtIndex:j] isEqualToString:@"End time:"]) {
[timesTitles removeObjectAtIndex:j];
}
}
[timesTitles addObjectsFromArray:daysToAddToTitlesArray];
NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:2],
[NSIndexPath indexPathForRow:1 inSection:2],
nil];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
for (int i = 2; i < [timesTitles count]; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:2];
[insertIndexPaths addObject:path];
}
UITableView *tv = (UITableView *)self.view;
[tv beginUpdates];
[tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tv endUpdates];
}
Этот код успешно удаляет 2 средних клеток.
Когда я иду добавить 2 новые клетки ниже остающегося 2 с помощью:
for (int i = 0; i < daysToAddToTitlesArray.count; i++) {
if (![timesTitles containsObject:[daysToAddToTitlesArray objectAtIndex:i]]) {
NSIndexPath *path = [NSIndexPath indexPathForRow:[timesTitles count] inSection:2];
[timesTitles insertObject:[daysToAddToTitlesArray objectAtIndex:i] atIndex:[timesTitles count]];
[insertIndexPaths addObject:path];
}
}
UITableView *tv = (UITableView *)self.view;
[tv beginUpdates];
[tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[tv endUpdates];
Он успешно добавляет новые клетки 2 ниже, но они ЪоТ есть текстовые поля в них.
Единственный выход, когда я запускаю приложение: log и log2.
Я так смущен.
Эй. Большое спасибо за ваш комментарий. В конце концов я получил его, добавив инструкцию else, содержащую «cell.accessoryView = nil», где бы я не захотел иметь аксессуар. – Michael
Рад, что я мог бы помочь: D Если вы довольны моим ответом, вы можете его проголосовать: D –