1

我根据这个苹果教程制作了索引列表,但我在删除项目时遇到了问题。当我删除底部部分中的最后一项时,部分标题仍然存在。这只发生在最后一节。

在此处输入图像描述

这是我删除项目的代码:

- (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];

}
4

1 回答 1

0

问题出在方法numberOfSectionsInTableView:上。当您删除给定部分中的所有项目时,该数字应该会发生变化。这是您可以执行的操作的示例:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger sections = 0;
    for (NSArray *sectionArray in littleWords) {
        if (sectionArray.count > 0) {
            sections++;
        }
    }
    return sections;
}

编辑:删除行后,您可能需要调用[tableView reloadData]或类似的方法来更新部分。

于 2013-08-06T02:38:35.437 回答