2

now I'm trying to animate the selected cell to move to the top, that means first position with sliding up and down animation.

If the tableview had more than two items, the user click the second one or third one, the selected cell will move to the first position, the first one comes to second and second one comes to third.

I have search and implement with some examples, but I have only move with duplicate cell one.

Here below is my code,

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath = %d",indexPath.row);
    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationTop];
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationTop];
    // update your dataSource as well.
    [tableView endUpdates];
    [coachInfoTableView reloadData];
}

If you any body know the solution for this one kindly update me. I will really appreciate you.

raman
  • 175
  • 1
  • 2
  • 14

3 Answers3

1

You have to update the datasource before making the animation and if you just want to move the cell, use - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath instead of delete/insert cell.

Calling reloadData will made you table view reload without any animation.

jasondinh
  • 918
  • 7
  • 21
0

What you are looking for is you want to re-order the tableview cells.

You can achieve this by using two important UITableView data source protocol methods:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

For tutorial and code see HERE. It will give you the animation effect like you mentioned, no need to worry about the cellForRowAtIndex delegate method.

GOOD LUCK !

Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29
-1

It seems that the call [coachIng TableView reloadData] is redundant.

[UITableView reloadData] reloads everything from scratch with original order.

user3687611
  • 31
  • 1
  • 4