今天發現一個問題搞了一個下午,異步網絡加載和表格刷新控件混用的時候,懶加載出現了問題
之前我是這樣寫的
/** * 初始化表格 */ - (UITableView *)tableView { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, TABLETOPVIEW_HEIGHT, SCREEN_WIDTH, TABLEVIEW_HEIGHT) style:UITableViewStylePlain]; [_tableView setDataSource:self]; [_tableView setDelegate:self]; [_tableView setBackgroundColor:NORMAL_BACKGROUND_COLOR]; [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; [_tableView setSeparatorInset:UIEdgeInsetsZero]; [ToolFunClass setExtraCellLineHidden:_tableView]; __weak typeof(self) weakSelf = self; // 添加傳統的下拉刷新 // 設置回調(一旦進入刷新狀態就會調用這個refreshingBlock) [_tableView addLegendHeaderWithRefreshingBlock:^{ [weakSelf.tableView.footer resetNoMoreData]; [weakSelf loadNewData]; }]; // 添加傳統的上拉刷新 // 設置回調(一旦進入刷新狀態就會調用這個refreshingBlock) [_tableView addLegendFooterWithRefreshingBlock:^{ [weakSelf loadMoreData]; }]; return _tableView; }
然後我發現我的表格下拉刷新動畫一直卡着,就是不加載數據,上網查了下原因是由於懶加載沒有判斷控件是否已經加載,導致重複執行兩次init事件,重置了表格的header.state,所以解決方法就是加上
/** * 初始化表格 */ - (UITableView *)tableView { //加上空值判斷,防止重複init if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, TABLETOPVIEW_HEIGHT, SCREEN_WIDTH, TABLEVIEW_HEIGHT) style:UITableViewStylePlain]; [_tableView setDataSource:self]; [_tableView setDelegate:self]; [_tableView setBackgroundColor:NORMAL_BACKGROUND_COLOR]; [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; [_tableView setSeparatorInset:UIEdgeInsetsZero]; [ToolFunClass setExtraCellLineHidden:_tableView]; __weak typeof(self) weakSelf = self; // 添加傳統的下拉刷新 // 設置回調(一旦進入刷新狀態就會調用這個refreshingBlock) [_tableView addLegendHeaderWithRefreshingBlock:^{ [weakSelf.tableView.footer resetNoMoreData]; [weakSelf loadNewData]; }]; // 添加傳統的上拉刷新 // 設置回調(一旦進入刷新狀態就會調用這個refreshingBlock) [_tableView addLegendFooterWithRefreshingBlock:^{ [weakSelf loadMoreData]; }]; } return _tableView; }