0

セクション ヘッダーをクリックしてテーブルビューを更新しようとすると、このアサーションが発生します。

* -[UITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit_Sim/UIKit-2839.5/UITableView.m:1264 でのアサーションの失敗

セクション ヘッダー ビューをクリックするたびに、カスタム セルを表示または非表示にしようとしています。

更新コードをリロード データに置き換えると、コードは正常に動作します。しかし、それはスムーズではありません:(

- (void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)section
{
    UTNoteItem* noteItem = self.notes[section];
    BOOL alreadySelected = noteItem.selected;
    if (alreadySelected) {
        self.selectedSection = NSNotFound;
        [self setSelected:NO forSection:section];
    }
    else {
        self.selectedSection = section;
        [self setSelected:YES forSection:section];
    }

    [self updateSections];
}

- (void)setSelected:(BOOL)selected forSection:(NSInteger)section
{
    UTNoteItem* noteItem = self.notes[section];
    noteItem.selected = selected;
    for (UTNoteItem* tmpItem in self.notes) {
        if (tmpItem != noteItem) {
            tmpItem.selected = NO;
        }
    }
}

- (void)updateSections
{
    NSMutableArray* deletePaths = [[NSMutableArray alloc] init];
    NSMutableArray* addPaths = [[NSMutableArray alloc] init];


    for (UTNoteItem* item in self.notes) {
        if (item.selected) {
            [addPaths addObject:[NSIndexPath indexPathForRow:0 inSection:[self.notes indexOfObject:item]]];
        }
        else {
            [deletePaths addObject:[NSIndexPath indexPathForRow:0 inSection:[self.notes indexOfObject:item]]];
        }
    }

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:deletePaths withRowAnimation:YES];
    [self.tableView insertRowsAtIndexPaths:addPaths withRowAnimation:YES];
    [self.tableView endUpdates];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.notes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    UTNoteItem* itemNote = self.notes[section];
    if (itemNote.selected) return 1;
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

編集:

これが私の新しい実装です:

-

 (void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)section
{
    /* Check if a section is opened */
    if (self.selectedSection != NSNotFound) {
        /* A section is open, get the item */
        UTNoteItem* theItem = self.notes[self.selectedSection];
        /* if the item is the section opened, close it */
        if (self.selectedSection == section) {
            theItem.selected = NO;
            self.selectedSection = NSNotFound;

        }
        /* The item is not the section, so open it, and close the previous item */
        else {
            theItem.selected = YES;
            UTNoteItem* prevItem = self.notes[self.selectedSection];
            prevItem.selected = NO;
            self.selectedSection = section;
        }
    }
    /* Nothin is open, just open the section */
    else {
        self.selectedSection = section;
        UTNoteItem* openItem = self.notes[self.selectedSection];
        openItem.selected = YES;
    }
    /* Reload the selected section.. this will not reload the other sections? */
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
4

1 に答える 1

1

同様の問題がありましたが、次のようにリロードを実行します。

- (void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)section
 {
        //check our action
        if(<will hide section>) {
            //hide section
            <some action>
        } else {
             //show section
             <some action>
        }
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

強制更新で別の方法で再度リロードします。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger nRows = 0; //no rows when closed

    if(<check section is open>) {
        nRows +=<number of data items shown>;
    }

    return nRows;
}

indexpath.section は、非表示または表示するセクションです。滑らかで安定しています。私の意見では、行の削除と追加は少し危険です。テーブルビューは、個々のセクションまたはセルでアニメーション化されたリロードを行うのに非常に適しています。

于 2013-08-02T00:48:07.407 に答える