刚有人问我,tableview的数据源更新的时候能否动画更新cell,reload刷新会有闪屏现象..答案是肯定的.
所以中午花了几分钟写了一个demo,无法上传工程.贴出代码...其实很简单...关键的就是下面两个方法的实现
16 - (void)addOneCell:(id)sender;
17 - (void)removeOneCell:(id)sender;
9 #import "ViewController.h" 10 #define NAVIGATION_HEIGHT 44 11 12 @interface ViewController (private) 13 - (void)initTableView; 14 - (void)initDataArray; 15 - (void)initNavigationBarButton; 16 - (void)addOneCell:(id)sender; 17 - (void)removeOneCell:(id)sender; 18 @end 19 20 @implementation ViewController 21 @synthesize myTableView,myDataArr; 22 23 - (void)viewDidLoad 24 { 25 [super viewDidLoad]; 26 // Do any additional setup after loading the view, typically from a nib. 27 [self initNavigationBarButton]; 28 [self initDataArray]; 29 [self initTableView]; 30 } 31 32 - (void)viewDidUnload 33 { 34 [super viewDidUnload]; 35 // Release any retained subviews of the main view. 36 } 37 38 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 39 { 40 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 41 } 42 43 #pragma mark - Initialize Method 44 - (void)initTableView 45 { 46 UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - NAVIGATION_HEIGHT) style:UITableViewStylePlain]; 47 _tableView.delegate = self; 48 _tableView.dataSource = self; 49 UIView *footerView = [[UIView alloc] init]; 50 [_tableView setTableFooterView:footerView]; 51 [footerView release]; 52 self.myTableView = _tableView; 53 [_tableView release]; 54 55 [self.view addSubview:self.myTableView]; 56 } 57 58 - (void)initDataArray 59 { 60 NSMutableArray *_tempArr = [NSMutableArray arrayWithCapacity:0]; 61 for (int a = 0; a < 10; a++) { 62 NSString *str = [NSString stringWithFormat:@"num:%d",a]; 63 [_tempArr addObject:str]; 64 } 65 self.myDataArr = _tempArr; 66 } 67 68 - (void)initNavigationBarButton 69 { 70 UIBarButtonItem *_addBarButton = [[UIBarButtonItem alloc] initWithTitle:@"add" style:UIBarButtonItemStylePlain target:self action:@selector(addOneCell:)]; 71 self.navigationItem.leftBarButtonItem = _addBarButton; 72 [_addBarButton release]; 73 74 UIBarButtonItem *_removeBarButton = [[UIBarButtonItem alloc] initWithTitle:@"remove" style:UIBarButtonItemStylePlain target:self action:@selector(removeOneCell:)]; 75 self.navigationItem.rightBarButtonItem = _removeBarButton; 76 [_removeBarButton release]; 77 } 78 79 #pragma mark - TableView Delegate 80 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 81 { 82 return [self.myDataArr count]; 83 } 84 85 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 86 { 87 static NSString *reuseID = @"Animation insert"; 88 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID]; 89 if (cell == nil) { 90 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID]; 91 } 92 cell.textLabel.text = [self.myDataArr objectAtIndex:indexPath.row]; 93 return cell; 94 } 95 96 #pragma mark - ButtonClick Method 97 - (void)addOneCell:(id)sender 98 { 99 [self.myTableView beginUpdates]; 100 [self.myDataArr insertObject:@"new" atIndex:0]; 101 NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; 102 [self.myTableView insertRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationNone]; 103 [self.myTableView endUpdates]; 104 } 105 106 - (void)removeOneCell:(id)sender 107 { 108 [self.myTableView beginUpdates]; 109 [self.myDataArr removeObjectAtIndex:0]; 110 NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; 111 [self.myTableView deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationFade]; 112 [self.myTableView endUpdates]; 113 } 114 115 #pragma mark - Dealloc 116 - (void)dealloc 117 { 118 self.myTableView = nil; 119 self.myDataArr = nil; 120 [super dealloc]; 121 } 122 123 @end