繁体   English   中英

索引列表中的僵尸节标题

[英]Zombie section headers in indexed list

我根据此苹果教程制作了索引列表,但是删除项目时遇到问题。 当我删除底部的节中的最后一项时,节标题保留在那里。 这仅在最后一节中发生。

在此处输入图片说明

这是我删除项目的代码:

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

}

问题出在方法numberOfSectionsInTableView: 删除给定部分中的所有项目时,编号应更改。 这是您可以做什么的示例:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM