简体   繁体   中英

Why am I getting an assert Error When trying to Delete a section in a tableview ios

I have a tableView with some sections, which all have a footer, and then I have a tableViewFooter on the Tableview itself.

If I scroll down to the bottom of my tableview and delete the last item(therefore deleting the section altogether) in any sections above the last section (second last and up) it gives me this error

2014-02-21 13:19:55.066 xxxx[5436:60b] *** Assertion failure in -[UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:], /SourceCache/UIKit/UIKit-2903.23/UITableViewSupport.m:2661
Uncaught exception: Cell animation stop fraction must be greater than start fraction

at endUpdates

this is my code

[self.tableView beginUpdates];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if(indexPath != nil){
        TableSection * sec = [self.sections objectAtIndex:indexPath.section];
        NSMutableDictionary *dic =[sec.items objectAtIndex:indexPath.row];

        Product* product = [dic valueForKey:PRODUCT];


        //removing the item in the section
        [sec.items removeObject:dic];

        //deleting item from products 
        NSMutableArray *temp = [NSMutableArray array];
        for (Product *p in self.dataCon.listPersister.products) {
            if ([p.product.objId isEqualToString: product.product.objId]) {
                [temp addObject:p];
            }
        }

        for (Product *p in temp) {
            [self.dataCon.listPersister.products removeObject:p];
        }


        //if it was the last object in section, delete the section else just delete the single row

        if(sec.items.count == 0)
        {


            [self.sections removeObject:sec];
            [self.footers removeObjectAtIndex:indexPath.section];
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]  withRowAnimation:UITableViewRowAnimationFade];

        } else
        {

            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            USFooterView *footer = [self.footers objectAtIndex:indexPath.section];
            footer.totalLabel.text = [self.dataCon.listPersister getTotalForShopId:sec.title];
            self.footerView.totalLabel.text = [self.dataCon.listPersister getTotalForAllProducts];
        }

    }


    [self.tableView endUpdates];

I had the same code earlier, just without my tableView and table sections having footers, where it worked, so I think they might be the problem, but I'm not entirely sure that's the reason it's acting up.

I have seen this post

UITableView tableFooterView may cause crash?

And the post that it links to, but that didn't help me.

Any help is appreciated :)

In the else statement you delete row from table view:

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

But not from data source. Delete row from array which you use as data source and it should works.

I found a "fix", but I'm avoiding the use of sectionFooter, because that seems to be bugged.

I created an extra cell at the end of each section, with the same setup I had for my footer View before, and made that last cell not editable with

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableSection * sec = [self.sections objectAtIndex:indexPath.section];
    if (sec.items.count != indexPath.row) {
        return YES;
    } else
        return NO;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        return [sec.items count] +1 ;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"normalcell";
static NSString *CellIdentifier1 = @"footercell";


TableSection * sec = [self.sections objectAtIndex:indexPath.section];

if (indexPath.row != sec.items.count) {
    //use normal type of cell


    return cell;
} else{
    //use footer type of cell

    return cell;
}

}

So the last cell Imitates a "footer", but it's not stuck to the bottom of the frame, but I'll have to live with that. It's better than crashes.

Try using UITableViewRowAnimationLeft or UITableViewRowAnimationRight as the delete row animation(deleteRowsAtIndexPaths:withRowAnimation:).

It crashed for me when using UITableViewRowAnimationAutomatic, but not with the other two. I have not tried all of them but it seems to be a bug with the animation code for some of the options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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