Assuming you have basic table with data set up, this is how you do row animations on it.
To remove a row from the end
1 2 3 4 5 6 7 8 9 10 |
-(void) removeButtonClicked:(UIButton*)sender { NSLog(@"you clicked on button %@", sender.titleLabel.text); UITableView * subMenu = (UITableView*)[self viewWithTag:100]; [self.dataArray removeLastObject]; [subMenu deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataArray.count inSection:0]] withRowAnimation:UITableViewRowAnimationTop]; } |
To add a row to the end
1 2 3 4 5 6 7 8 9 10 |
-(void) addButtonClicked:(UIButton*)sender { NSLog(@"you clicked on button %@", sender.titleLabel.text); [self.dataArray addObject:[NSString stringWithFormat:@"%lu", self.dataArray.count + 1]]; UITableView * subMenu = (UITableView*)[self viewWithTag:100]; //append row [subMenu insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop]; } |
To reload all sections in a table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
-(void) reloadButtonClicked: (UIButton*)sender { // no animation NSLog(@"you clicked on button %@", sender.titleLabel.text); UITableView * subTableView = (UITableView*)[self viewWithTag:100]; for (int i = 0; i < self.dataArray.count; ++i) { // Select a random element between i and end of array to swap with. int Elements = (int)self.dataArray.count - i; int n = (arc4random() % Elements) + i; [self.dataArray exchangeObjectAtIndex:i withObjectAtIndex:n]; } NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [subTableView numberOfSections])]; [subTableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade]; } |
Move a row
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// move data from the FROM location to the TO location -(void) moveButtonClicked:(UIButton*)sender { NSLog(@"you clicked on button %@", sender.titleLabel.text); UITableView * subMenu = (UITableView*)[self viewWithTag:100]; // get the to location's data at the FROM id data = [self.dataArray objectAtIndex:0]; // remove the that data [self.dataArray removeObject:data]; // then insert the data to the TO location [self.dataArray insertObject:data atIndex:self.dataArray.count]; // then just call the moveRowAtIndexPath [subMenu moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]]; } |