3

In my UITableView instance, I'm moving the cell to index:0 and reloading the data. The problem is that the cell "snaps" to the top (due to the reloadData, i think). I'm wondering if i can move the cell up, then apply reloadData a few seconds later. Heres my code:

[_toDoItems removeObject:todoItem];

[_toDoItems insertObject:todoItem atIndex:0 ];





[self.tableView beginUpdates];

[self.tableView moveRowAtIndexPath:origIndexPath toIndexPath:0];

[self.tableView endUpdates];

sleep(1);
[self.tableView reloadData];

the problem with this is, after the method is called, everything pauses for 1 second, then the "snap" animation occurs.

Noel M
  • 15,812
  • 8
  • 39
  • 47
Brian
  • 73
  • 2
  • 11
  • 1
    Whats the use of `sleep(1);`? Remove it. – Souljacker Aug 05 '13 at 14:51
  • 3
    `sleep()` should never be used. It blocks all threads including the main one which handles all of the visuals. This is why you see it waiting a second, then snapping. The snapping is just waiting for its tread to be unlocked. Also since `reloadData` does not have any animations it will give a "snapping" effect. – Firo Aug 05 '13 at 15:02
  • my logic was, that the cell would get moved to the top; then it the sleep would delay the reloadData 1 second. – Brian Aug 05 '13 at 15:47

3 Answers3

1

You first tell the tableView to expect updates with beginUpdates. Then update the relevant sections (if your tableView has only one section then just pass zero for the section number). Here's where you specify the animation - you can play around with it to get the effect that you want. After that you call endUpdates on the tableView. The typedef for UITableViewRowAnimation specifies:

typedef enum {
   UITableViewRowAnimationFade,
   UITableViewRowAnimationRight,
   UITableViewRowAnimationLeft,
   UITableViewRowAnimationTop,
   UITableViewRowAnimationBottom,
   UITableViewRowAnimationNone,
   UITableViewRowAnimationMiddle,
   UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Play around to see which one you want. Even selecting UITableViewRowAnimationNonecan have a nice effect sometimes. Depending on how many sections you have you can use - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

Table update code below:

[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
icodebuster
  • 8,890
  • 7
  • 62
  • 65
  • @brain If you find any of the answers useful please **upvote it** and if answer is correct then please **accept it** this will help to remove the question from the list of **unanswered list of question.** – icodebuster Aug 16 '13 at 14:56
1

Solved.

     [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(briefPause) userInfo:nil repeats:NO];

I used the above in the same method as

[self.tableView beginUpdates]
....

but I made a new function, briefPause which is just:

    [self.tableView reloadData];
Brian
  • 73
  • 2
  • 11
  • It actually works but i would recommend to use less time: [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(briefPause) userInfo:nil repeats:NO]; It will still work, but look smoother – Renexandro Nov 24 '13 at 18:12
0

Try this:

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:origIndexPath
                                                       inSection:0]]
                 withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0
                                                       inSection:0]]
                 withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • 2
    And as others have mentioned above, don't use sleep(). I forbid it! :) – Guy Kogus Aug 05 '13 at 15:04
  • even tweaking withRowAnimation: to something else, it doesnt really fix the problem. It prevents the weird snap/flicker but the cells just instantaneously appear at the top without animation. :/ – Brian Aug 05 '13 at 15:45