1

I am having a UITableView inside a scrollview. So I am trying to do a accordion like component using UITableView. So I need to expand the UITableView to add more cell. In that case I have to increase the height of UIScrollView with a animation so that it matches the table animation.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    BOOL preventReopen = NO;

    if (row == expandedRowIndex + 1 && expandedRowIndex != -1)
        return nil;

    [tableView beginUpdates];
    if (expandedRowIndex != -1)
    {
        familyTableView.frame = CGRectMake(familyTableView.frame.origin.x, familyTableView.frame.origin.y, familyTableView.frame.size.width, 3*50+22);

        toolsTableView.frame = CGRectMake(toolsTableView.frame.origin.x, familyTableView.frame.origin.y + familyTableView.frame.size.height + 20, toolsTableView.frame.size.width, 4*50+22);

        myAccountTableView.frame = CGRectMake(myAccountTableView.frame.origin.x, toolsTableView.frame.origin.y + toolsTableView.frame.size.height + 20, myAccountTableView.frame.size.width, 2*50+22);

        settingsScrollView.contentSize = CGSizeMake(320, familyTableView.frame.size.height + toolsTableView.frame.size.height+ myAccountTableView.frame.size.height + 380);
        settingsScrollView.contentInset = UIEdgeInsetsMake(0, 0, familyTableView.frame.size.height + toolsTableView.frame.size.height+ myAccountTableView.frame.size.height + 380, 0);

        NSInteger rowToRemove = expandedRowIndex + 1;

        preventReopen = row == expandedRowIndex;
        if (row > expandedRowIndex)
            row--;
        expandedRowIndex = -1;
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    }else {
        familyTableView.frame = CGRectMake(familyTableView.frame.origin.x, familyTableView.frame.origin.y, familyTableView.frame.size.width, (3*50+22) + 100);

        toolsTableView.frame = CGRectMake(toolsTableView.frame.origin.x, familyTableView.frame.origin.y + familyTableView.frame.size.height + 20, toolsTableView.frame.size.width, 4*50+22);

        myAccountTableView.frame = CGRectMake(myAccountTableView.frame.origin.x, toolsTableView.frame.origin.y + toolsTableView.frame.size.height + 20, myAccountTableView.frame.size.width, 2*50+22);

        settingsScrollView.contentSize = CGSizeMake(320, familyTableView.frame.size.height + toolsTableView.frame.size.height+ myAccountTableView.frame.size.height + 380);
        settingsScrollView.contentInset = UIEdgeInsetsMake(0, 0, familyTableView.frame.size.height + toolsTableView.frame.size.height+ myAccountTableView.frame.size.height + 380, 0);
    }
    NSInteger rowToAdd = -1;
    if (!preventReopen)
    {
        rowToAdd = row + 1;
        expandedRowIndex = row;
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToAdd inSection:0]] withRowAnimation:UITableViewRowAnimationTop];

    }
    [tableView endUpdates];

    return nil;
}

I am not good at animation. Any help is greatly appreciated. What I need is when we do [tableView beginUpdates] we have to start animating the resize effect to UIScrollView and it should end when [tableView endUpdates] executes. So the Accordion executes flawlessly. Thanks in advance for any help..

4

2 回答 2

1

Since you are changing a view property (the frame of the scroll view) I would recommend that you use a UIView animation to animate the frame. You may need to tweak the timing slightly to make it look perfect but I believe that the animation is both EaseInEaseOut and takes 0.4 seconds. You would to the animation like this:

[UIView animateWithDuration:0.4 animations:^{
    // Change all your animatable properties like your frames here...
    yourScrollView.frame = theNewScrollViewFrame;
}];
于 2012-06-04T13:47:26.093 回答
1

I finally fixed it by following code

// Start the animation before the begin update and commit it after the end update.        
[UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];

        [tableView beginUpdates];
<My code which I given in the question>
       [tableView endUpdates];
        [UIView commitAnimations];

Hope it helps some one in future..

于 2012-06-08T10:36:37.477 回答