Expand and Shrink UIScrollView with animation
- by Dilip Rajkumar
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..