1 /* 2 实现tableview的下拉刷新 3 tableview滑动就会触发这个方法? 4 */ 5 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 6 { 7 //当tableview下拉到最后一行的时候才触发 8 if (indexPath.row == self.m_data.count - 1) { 9 10 //定义一个UIView 11 UIView *footSpinnerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 60.0f)]; 12 13 //顶一个有刷新图标的view 14 UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 60.0f, 60.0f)]; 15 activity.color = [UIColor redColor]; 16 [activity startAnimating];//启动有刷新图标的view 17 18 footSpinnerView.backgroundColor = [UIColor grayColor]; 19 [footSpinnerView addSubview:activity]; 20 21 //设置footerview 22 self.myTableView.tableFooterView = footSpinnerView; 23 24 // self.myTableView.tableHeaderView = footSpinnerView; 25 26 dispatch_queue_t queue = dispatch_queue_create("my queue", nil); 27 28 //在后台线程添加数据 29 dispatch_async(queue, ^(void){ 30 31 [self.m_data addObject:@"1000"]; 32 [self.m_data addObject:@"1001"]; 33 [self.m_data addObject:@"1002"]; 34 [self.m_data addObject:@"1003"]; 35 [self.m_data addObject:@"1004"]; 36 37 }); 38 39 //添加完数据就重新加载数据 40 dispatch_async(queue, ^(void) { 41 42 sleep(2); 43 dispatch_sync(dispatch_get_main_queue(), ^(void){ 44 45 [self.myTableView reloadData]; 46 }); 47 }); 48 49 // [self.myTableView reloadData]; 50 dispatch_release(queue); 51 [footSpinnerView release]; 52 [activity release]; 53 } 54 // else if (indexPath.row == 0) { 55 // 56 // UIView *footSpinnerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 60.0f)]; 57 // UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 60.0f, 60.0f)]; 58 // activity.color = [UIColor redColor]; 59 // [activity startAnimating]; 60 // 61 // footSpinnerView.backgroundColor = [UIColor grayColor]; 62 // [footSpinnerView addSubview:activity]; 63 // 64 // // self.myTableView.tableFooterView = footSpinnerView; 65 // 66 // self.myTableView.tableHeaderView = footSpinnerView; 67 // 68 // dispatch_queue_t queue = dispatch_queue_create("my queue", nil); 69 // 70 // dispatch_async(queue, ^(void){ 71 // 72 // [self.m_data insertObject:@"1000" atIndex:0]; 73 //// [self.m_data addObject:@"1001"]; 74 //// [self.m_data addObject:@"1002"]; 75 //// [self.m_data addObject:@"1003"]; 76 //// [self.m_data addObject:@"1004"]; 77 // // [self.m_data insertObject:[NSArray arrayWithObject:@"01"] atIndex:0]; 78 // 79 // }); 80 // 81 // dispatch_async(queue, ^(void) { 82 // 83 // sleep(2); 84 // dispatch_sync(dispatch_get_main_queue(), ^(void){ 85 // 86 // [self.myTableView reloadData]; 87 // [self.myTableView scrollToRowAtIndexPath:[indexPath initWithIndex:3] atScrollPosition:UITableViewScrollPositionNone animated:YES]; 88 // }); 89 // }); 90 // 91 // 92 // dispatch_release(queue); 93 // [footSpinnerView release]; 94 // [activity release]; 95 // } 96 97 //如果不是最后一行,则把footerview和headerview都设为nil 98 else 99 { 100 self.myTableView.tableFooterView = nil; 101 self.myTableView.tableHeaderView = nil; 102 } 103 }