我的 UITableview
有问题。滚动或按下开关后,它有时会开始混淆单元格。因此,您可以让一个单元格的文本显示在另一个单元格中。我阅读了很多有关 tableViews 的内容,但没有找到适合我的代码的内容。
在这里:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = NSLocalizedString(@"Course:",@"Course Section");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = entryTableCourseName;
}
else if (indexPath.row == 1) {
cell.textLabel.text = @"Due Date:";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = entryTableDueDateString; }
}
else if (indexPath.section == 1) {
if (indexPath.row == 0) {
//cell.textLabel.text = NSLocalizedString(@"Title", @"entryTableTaskTitle");
[cell.contentView addSubview:nameField];
}
else if (indexPath.row == 1) {
[cell.contentView addSubview:TaskView];
}
}
else if (indexPath.section == 2) {
if (entryTableGradeSwitch.on) {
if (indexPath.row == 0) {
cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
cell.accessoryView = entryTableGradeSwitch;
}
else if (indexPath.row == 1) {
cell.textLabel.text = NSLocalizedString(@"Options", @"GradingCellOptions");
}
}
else {
cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
cell.accessoryView = entryTableGradeSwitch;
}
}
}
我觉得我在设置单元格时做错了什么。
最佳答案
这全都归功于单元格的可重用性。 StackOverflow 上有很多关于此的帖子,我建议您搜索“UITableViewCell Reusability Problems”,您会找到解决方案。
简而言之 .. 这里发生的事情是,当您滚动时,单元格基本上会重新使用之前在该 indexPath 中使用的单元格,并且 cellForRowAtIndexPath 会在该索引路径中分配一个不同的单元格。
解决方案 ?
- 为每个单元格使用不同的重用标识符。
- 使用困惑的 View 或标签对 tableViewCell 进行子类化。
关于iphone - UITableview 混淆单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10918940/