【发布时间】:2010-12-15 13:26:44
【问题描述】:
我正在编写一个包含 2 个部分的 UITableView。当表格第一次加载时,所有单元格都显示正确的信息,但是当我开始向上和向下滚动时,单元格 detailTextLabel 和 accessoryType 被错误地刷新,因此一些应该只包含 detailTextLabel 的单元格也包含一个附件,而单元格应该只包含一个附件还包含一个详细的TextLabel。
在cellForRowAtIndexPath: 内部,我正在使用嵌套的 switch/case 语句将正确的值应用于各自部分/行中的单元格。据我所知,这些语句中的逻辑是正确的,那么更新时cell 变量的值是否可能是错误的?
表格加载正确,但在滚动附件类型和详细文本标签后会混淆。
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;
}
【问题讨论】:
标签: iphone objective-c uitableview tableview