我正在编写一个包含 2 个部分的 UITableView。当表格第一次加载时,所有单元格都显示正确的信息,但是当我开始上下滚动时,单元格 detailedTextLabel 和 accessoryType 被错误地刷新,这样一些应该只包含 detailedTextLabel 的单元格也包含一个附件,而单元格应该只包含一个附件也包含一个 detailedTextLabel。
在 cellForRowAtIndexPath:
内部,我使用嵌套的 switch/case 语句将正确的值应用到各自部分/行中的单元格。据我所知,这些语句中的逻辑是正确的,那么 cell
变量的值是否有可能在更新时不正确?
表格加载正确,但滚动后 accessoryType 和 detailedTextLabel 混淆了。
Click for link to screen shots of the table.
这是我的 UITableViewController 子类中的代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sectionNames count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *headingsSection = [cellTitles objectAtIndex:section];
return [headingsSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionNames objectAtIndex:section];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
self.tableView.allowsSelection = YES;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
switch (indexPath.section) {
case 0:
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
break;
case 1:
switch (indexPath.row) {
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
if (defaultAssistOn) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
break;
}
break;
}
return cell;
}
最佳答案
由于单元格的重复使用,具有先前设置值的单元格重新出现。
在 switch (indexPath.section) ...
之前,将 detailTextLabel 和 accessoryType 初始化为默认值:
cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryNone;
https://stackoverflow.com/questions/4450366/