我根据这个苹果教程制作了索引列表,但我在删除项目时遇到了问题。当我删除底部部分中的最后一项时,部分标题仍然存在。这只发生在最后一节。
这是我删除项目的代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self.littleWords objectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
littleWords 是与苹果教程中的状态相同的部分数组。
制作细胞的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MyBasicCell"];
Word * myWord = [[self.littleWords objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = myWord.name;
cell.detailTextLabel.text = myWord.translation;
return cell;
}
其余的 tableView 功能:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.littleWords count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.littleWords objectAtIndex:section] count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([[self.littleWords objectAtIndex:section] count] > 0) {
NSString *str = [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
NSLog(@"returning section header: %@", str);
return str;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}