Разделы UitableView перезаписываются на iPhone?
-
26-10-2019 - |
Вопрос
У меня есть одна проблема с двумя разделами в стиле сгруппированного стиля UitableView. Первый раздел с 6 рядами и вторым разделом с 3 рядами. Когда я прокручиваю обзор таблицы вверх и вниз, последнее содержание строки в разделе 2 добавляет в первом разделе «Первая строка», а содержимое первой строки в первом разделе добавляет в последнюю строку второго раздела. Я лучше всего проверяю свой уровень, станет ли ячейка пустой, чтобы загрузить контент, но это не происходит при отладке. Мой код,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor whiteColor];
if (indexPath.section == 0 && indexPath.row == 0)
{
UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
Nameabel.text = @"Name";
Nameabel.font = [UIFont fontWithName:@"Helvetica" size:15];
Nameabel.backgroundColor = [UIColor clearColor];
Nameabel.font = [UIFont boldSystemFontOfSize:15];
[cell.contentView addSubview:Nameabel];
textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
textField.placeholder = @"Student Name";
textField.borderStyle = UITextBorderStyleNone;
textField.font = [UIFont fontWithName:@"Helvetica" size:14];
[cell.contentView addSubview:paymentCatTextField];
}
else if (indexPath.section == 1 && indexPath.row == 0)
{
UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
RLabel.text = @"Class";
RLabel.backgroundColor = [UIColor clearColor];
RLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
RLabel.font = [UIFont boldSystemFontOfSize:15];
[cell.contentView RLabel];
RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
RTextField.placeholder = @"class Name";
RTextField.borderStyle = UITextBorderStyleNone;
RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
RTextField.font = [UIFont fontWithName:@"Helvetica" size:14];
[cell.contentView addSubview:RTextField];
}
}
return cell;
}
Это для образца. Таким образом, в разделе 0 есть 6 строк, а в разделе 1 есть 3 ряда. Где я делаю неправильно. Пожалуйста, помогите моей, чтобы решить эту проблему.
Заранее спасибо.
Решение
Измените свой код на следующее и попробуйте еще раз:
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor whiteColor];
}
if (indexPath.section == 0 && indexPath.row == 0) {
//bla bla
} else if (indexPath.section == 1 && indexPath.row == 0) {
//bla bla
}
return cell;
}
Причина в том, что вы устанавливаете содержание ячейки только тогда, когда вы не используете ячейку. Это неправильно в большинстве случаев.
Другие советы
В дополнение к ответу Дасде, я бы также предложил бы удалить подвесители, прежде чем добавить этикетки.
if ([[cell.contentView subviews] count] > 0) {
UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
[labelToClear removeFromSuperview];
UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1];
[textToClear removeFromSuperview];
}
и авторелирование подвесителей:
UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease];
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];