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 }