ios - 更新uitableview时断言失败

标签 ios objective-c runtime-error

当我单击节标题时尝试更新我的表格 View 时,我收到此断言。

* -[UITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit_Sim/UIKit-2839.5/UITableView.m:1264 中断言失败

每当我单击节标题 View 时,我只是尝试隐藏和显示自定义单元格。

如果我用重新加载数据替换更新代码,代码可以正常工作。但这并不顺利:(

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

最佳答案

我遇到了类似的问题,但是我像这样执行了重新加载:

- (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是我希望隐藏或显示的部分。它光滑稳定。 在我看来,删除和添加行有点危险,表格 View 非常擅长对各个部分或单元格进行动画重新加载。

关于ios - 更新uitableview时断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006959/

相关文章:

ios - 调整 GLKView 的大小

ios - 从包含的 UIViewController 向 UIView 的 UIButton 分配回调

ios - iPad 上的 iCarousel 性能问题

linux - gnuplot:没有足够的列用于可变颜色

ios - UITabBar 禁用/启用 UIButton 单击上的项目

objective-c - iOS:即使用户当前语言不是英语,也使用 en.lproj 资源

iOS UITapGestureRecognizer 有时不工作

c - 为什么这个程序会在执行时崩溃?

c++ - 错误 : Run-Time Check Failure #2 - Stack around the variable 'file' was corrupted

objective-c - iOS 应用程序启动时间太长无法显示