2014-01-13 61 views
0

我有一個tableView它填充了一些NSMutableArray。 後來當我對數組進行排序時,我希望表視圖將每一行緩慢移動到新的位置,慢慢地,我的意思是大約0.3秒,以便您可以看到開關。排序行時緩慢動畫tableView

我嘗試了這些(含各種動畫(頂部,左,褪色等):

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop]; 

和它不是好像我在這裏所描述的,但有些重裝不清楚 其實我。希望看到的第4排走一路向上是第2行,當它與它的方式行更換地方。 這可能嗎?

+0

請完全標記,因爲Objective-C也適用於OSX。我標記了'ios'和'uitableview',爲此我必須閱讀完整的問題,最後在滾動到右後我才知道你想**'ios' ** –

回答

0

你需要明確地告訴tableview哪一行應該在哪裏移動。我使用下面的代碼,這對我很好。在這種情況下,「ObjectMap」是我自己的類,它將對象映射到數字。有點像NSDictionary,但使用對象指針進行比較。

NSArray *originalList = ... 
    ObjectMap *map = [ObjectMap objectMap]; 
    NSUInteger index = 0; 
    for (Waypoint *waypoint in originalList) { 
     [map setObject:[NSNumber numberWithInteger:index++] forObjectKey:waypoint]; 
    } 

    NSArray *sortedList = ... // sorted version of the originalList 

    // do the animations 
    [CATransaction begin]; 
    [CATransaction setCompletionBlock:^{ 
     [theTableView reloadData]; 
    }]; 

    [theTableView beginUpdates]; 
    index = 0; 
    for (Waypoint *waypoint in sortedList) { 
     NSNumber *originalIndex = [map objectForKey:waypoint]; 
     [theTableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:[originalIndex integerValue] inSection:0] 
          toIndexPath:[NSIndexPath indexPathForRow:index++ inSection:0]]; 
    } 
    [theTableView endUpdates]; 

    [CATransaction commit]; 
0

您可以創建一個UITableView類別,並調用reloadDataAnimated:YES,這將增加重新載入表格數據時的淡入效果:

#import <QuartzCore/QuartzCore.h> 

@implementation UITableView (Animated) 

    - (void)reloadDataAnimated:(BOOL)animated 
    { 
     [self reloadData]; 

     if (animated) 
     { 
      CATransition *animation = [CATransition animation]; 
      [animation setType:kCATransitionFade]; 
      [animation setSubtype:kCATransitionFromBottom]; 
      [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
      [animation setFillMode:kCAFillModeBoth]; 
      [animation setDuration:.3]; 
      [[self layer] addAnimation:animation forKey:@"UITableViewReloadDataAnimationKey"]; 
     } 
    } 

    @end 
+0

他希望在單元格(UITableViewCells)上發生動畫而不是在tableView上。 – santhu

0

使用 -

(void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath

方法;
所以,如果你想從4移動到2,首先從4移動到3然後3移動到2,它會工作。