Tab-like-thing with tab animation in UITableView section

I have a UITableView with sections in an iPhone app where it looks like something similar to viewing contact details in a standard contacts app. In one of the sections, I set the UISegmentedControl as the title of the section, and the items in that section depend on which segment is selected in the segmented control. I would like to make the animations move the lines of one tab out of the view to one side and move the lines of the other tab on the other side. I managed to achieve this by inserting / deleting the appropriate rows with the default UITableView animation, but it looks a little strange when the number of rows in one tab is different from the others. Also the code for this is pretty nasty.

Here's the code I've simplified a bit:

[self.tableView beginUpdates];
UITableViewRowAnimation delAnim = UITableViewRowAnimationRight;
UITableViewRowAnimation insAnim = UITableViewRowAnimationLeft;
NSUInteger numdel = [self.list count];
NSUInteger numins = [newlist count];
NSMutableArray* indices = [NSMutableArray arrayWithCapacity: numdel];
for (NSUInteger i = 0; i < numins; i++)
{
    [indices addObject: [NSIndexPath indexPathForRow: i  inSection: SECT]];
}
[self.tableView insertRowsAtIndexPaths: indices  withRowAnimation: insAnim];
[indices removeAllObjects];
for (NSUInteger i = 0; i < numdel; i++)
{
    [indices addObject: [NSIndexPath indexPathForRow: i  inSection: SECT]];
}
[self.tableView deleteRowsAtIndexPaths: indices  withRowAnimation: delAnim];
self.list = newlist;
[self.tableView endUpdates];

      

I am new to Objective C and Cocoa, so any advice is greatly appreciated.

+1


a source to share


1 answer


You can try using a separate table view that you animate directly. The snippet here is just printed on the cuff, so it might take some work, but it should give you some pointers, at least:

- (void) switchToNewTableFromRight
{
    UITableView * newTableView = [[UITableView alloc] initWithFrame: self.tableView.frame style: self.tableView.style];

    // put it off to the right of the existing table
    CGRect frame = newTableView.frame;
    frame.origin.x += frame.size.width;
    newTableView.frame = frame;

    // set data for new table
    // you should ensure you're setup to supply data for the new table here, btw
    newTableView.delegate = self;
    newTableView.dataSource = self;
    [newTableView reloadData];

    // add to parent of current table view at this (offscreen) location
    [self.tableView.superview addSubview: newTableView];

    // now we animate
    [UIView beginAnimations: @"TableFromRight" context: newTableView];

    // set the function it should call when the animation completes
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector: @selector(animation:finished:context:)];

    // set new table frame to current table frame
    newTableView.frame = self.tableView.frame;

    // set current table frame to be offscreen to the left
    frame = self.tableView.frame;
    frame.origin.x -= frame.size.width;
    self.tableView.frame = frame;

    // commit the animations to start them going
    [UIView commitAnimations];
}

- (void) animation: (NSString *) animationID finished: (BOOL) finished context: (void *) context
{
    // could be a good idea to check that finished == YES here
    UITableView * newTableView = (UITableView *) context;
    self.tableView = newTableView;

    // newTableView has been inited but not autoreleased, etc.
    // now the controller (self) owns it, so release that first reference
    [newTableView release];
}

      

The idea here is that you set up a new table (making it the same size as the existing one), place it off-screen to the right of the existing table, and then animate the movement of both tables remaining to their width. Thus, the existing table will slide off the screen and the new one on the screen. When the animation finishes, it will call the provided method, giving you the option to make the new table view the official table view.



Another option would be to use a flip transition, which might look something like this:

// setup new table
UITableView * newTableView = [[UITableView alloc] initWithFrame: self.tableView.frame style: self.tableView.style];
newTableView.delegate = self;
newTableView.dataSource = self;
[newTableView reloadData];

[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: self.tableView.superview cache: YES];

// generally here you'd remove the old view and add the new view
// I'm *assuming* that UITableViewController -setTableView: will do the same thing
self.tableView = newTableView;

[UIView commitAnimations];

      

We hope one of them will have the desired effect.

+3


a source







All Articles