[摘要]本文是對實現tableview的下拉刷新的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
實現tableview的下拉刷新
推薦第三方下拉刷新代碼http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0/52326ce26803fabc46000000
tableview滑動就會觸發這個方法?
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//當tableview下拉到最後一行的時候才觸發
if (indexPath.row == self.m_data.count - 1) {
//定義一個UIView
UIView *footSpinnerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 60.0f)];
//頂一個有刷新圖標的view
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 60.0f, 60.0f)];
activity.color = [UIColor redColor];
[activity startAnimating];//啟動有刷新圖標的view
footSpinnerView.backgroundColor = [UIColor grayColor];
[footSpinnerView addSubview:activity];
//設置footerview
self.myTableView.tableFooterView = footSpinnerView;
// self.myTableView.tableHeaderView = footSpinnerView;
dispatch_queue_t queue = dispatch_queue_create("my queue", nil);
//在後台線程添加數據
dispatch_async(queue, ^(void){
[self.m_data addObject:@"1000"];
[self.m_data addObject:@"1001"];
[self.m_data addObject:@"1002"];
[self.m_data addObject:@"1003"];
[self.m_data addObject:@"1004"];
});
//添加完數據就重新加載數據
dispatch_async(queue, ^(void) {
sleep(2);
dispatch_sync(dispatch_get_main_queue(), ^(void){
[self.myTableView reloadData];
});
});
// [self.myTableView reloadData];
dispatch_release(queue);
[footSpinnerView release];
[activity release];
}
}