Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dantastic

macrumors 6502a
Original poster
Jan 21, 2011
572
678
Caught this one in testing... nasty.

I've a UITableView that I can add cells to. I've a button up in the navigation bar that calls this function:
Code:
- (IBAction)addCell {
	... create a new object to be added. 
	// Add it to the array containing the UITableView
	[myArray addObject:newObject];
	
	// Pop the new cell into the UITableview
	[self.tableView beginUpdates];
	[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[myArray count] - 1 inSection:3]] withRowAnimation:NO];
	[self.tableView endUpdates];

	// Scroll down to the newly created cell
	// Shift the view so the keyboard doesn't cover the cell. 
	[UIView beginAnimations: @"anim" context: nil];
	[UIView setAnimationBeginsFromCurrentState: YES];
	[UIView setAnimationDuration: 0.3f];
	[self.tableView setContentOffset:CGPointMake(0.0, [myArray count] - 1 * 44)];
	self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -keyboardOffset);
	[UIView commitAnimations];		
}

This all works fine as long as the table view is not scrolling at the time ob the *add* button being pressed. If the UITableView is currently being animated by the scrolling motion the scroll will somewhat pause for 0.1 sec. then my animation to shift the view work for like 0.1 sec. then the scrolling takes over again.

The problem is that I end up with a view in a state sometimes difficult to recover from.

I realize these animations are intercepting each other causing this. the preferred option would be if I could cancel the scrolling animation in progress and perform my own per the function above.

If that's a no-go - any better options of doing this?
 
Last edited:
Since UITableView is a subclass of UIScrollView, you can declare yourself as a delegate for the UIScrollViewDelegate protocol and use the scroll view's delegate methods.

One such method that may interest you is scrollViewDidEndScrollingAnimation: which will notify you when the view is done animating the scrolling animation.

One way you could go about using that is with some sort of temporary state variable set based on whether or not the dragging property of the table view is YES, meaning it's currently scrolling. Then you could choose not to start that animation in your addCell method, but instead start the animation in scrollViewDidEndScrollingAnimation: (as long as that temporary state variable says you can).

It's pretty hackish, but it should work.

http://developer.apple.com/library/...tml#//apple_ref/occ/intf/UIScrollViewDelegate
 
Not a bad idea but the user would have to wait until the tableview stops scrolling. I wouldn't be happy with that approach. Can't seem to find a way of actually stopping the scrolling so I'm pondering alternatives.

cheers!
 
Disabling scrolling will 'pause' the animation. It will continue from where it was stopped when you enable it again. I've tested that one already :rolleyes:

I think I have come up with a workaround though. I'll just put the button to add in a cell of its own down the bottom. That way the textField will be in view already so no scrolling needed.

It's a workaround but it's better than having a crappy app.
 
Disabling scrolling will 'pause' the animation. It will continue from where it was stopped when you enable it again. I've tested that one already :rolleyes:
Okay. I guess it would've helped us answer if you perhaps had let us know what you had already tried. If you haven't already, I would suggest reading Mike Ash's excellent blog article: Getting Answers.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.