簡體   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