Problem with RowAnimation

2019-06-20 02:30发布

Hey guys. i have a problem with animating deletes and inserts on a UITableView. The Fact ist, that i have different cellheights, some 44.0 some 135.0 now. i have uitableviewstylegrouped with different sections. the first row of each sections is a grouping row. on click i remove all rows of this section except the grouping row. but that animation looks weird, when animating (UITableViewRowAnimationTop) the 135px height cell. i tried to set self.tableview.cellheight to 135 and commented out the tableView:cellHeightForIndexPath-Method. And the Animation works fine. Or i set every cellheight to 135 in tableView:cellHeightForIndexPath-Method.

It looks like the animation process checks the height of the first row in the sections an takes that height of the cell for all following cells to animate.

Somebody an idea?


- (void) showhideGroup:(int)group
{   
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

    MCGroup *handleGroup = [groups objectAtIndex:group];
    NSMutableArray *groupRows = [visibleGroupData objectForKey:handleGroup.title];

    [self.tableView beginUpdates];

    if (!handleGroup.hidden) {
        int row = 0;
        for(MobileFieldMapping *field in groupRows)
        {
            if (![field.datatype matchesInsensitive:GROUPING_CELL]) 
            {
                NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:group];
                [indexPaths addObject:path];
            }
            row++;
        }

        row = 0;
        for(NSIndexPath *index in indexPaths)
        {
            [groupRows removeObjectAtIndex:index.row-row];
            row++;
        }
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
        handleGroup.hidden=YES;
    }
    else 
    {
        NSMutableArray *allGroupRows = [groupData objectForKey:handleGroup.title];

        int row = 0;
        for (MobileFieldMapping *field in allGroupRows) 
        {
            if (![groupRows containsObject:field]) 
            {
                NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:group];
                [indexPaths addObject:path];
                [groupRows insertObject:field atIndex:row];
            }
            row++;
        }

        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
        handleGroup.hidden=NO;
    }
    [self.tableView endUpdates];


    [indexPaths release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    if ([cell isKindOfClass:[FormCell class]]) 
    {
        return [(FormCell*)cell height];
    }

    return 44.0;
}

2条回答
祖国的老花朵
2楼-- · 2019-06-20 02:45

I had the same problem. My workaround is to use fade section animation:

[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

Instead of one for elements:

[tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];

This looks much smoother.

查看更多
Bombasti
3楼-- · 2019-06-20 02:52

An old one that is still unanswered. I assume you figured it out a long time ago, but here is my first thought. (I recall trying to do something like this myself.)

When the UITableView calls heightForRowAtIndexPath:, it will be because the table is trying to prepare the cell, so you can't use the cell to return the height. This may cause an infinite regression in calls or it may detect this and just give up or throw an exception and leave you holding the bag.

You must calculate the cell height without using the cell itself.

As for the cell height assumption, try calling insert/delete for individual cells rather than a single call to do it all at once. The UITableView will still batch them up for the animation after you call endUpdates.

查看更多
登录 后发表回答