1. 程式人生 > >iOS開發之tableview位置錯亂,偏移,回到原來位置

iOS開發之tableview位置錯亂,偏移,回到原來位置

上拉重新整理後發現數據位置錯亂?tableview的偏移不對?比如資料一共有40條,一次載入20條,正常應該顯示在第21條,上拉時載入後發現tableview停在了第11條或者30幾條,如果你發生了和我一樣的情況,那看這裡

- (void)addRefresh {

__weak typeof (self) weakSelf = self;
weakSelf.nomalTable.mj_header =  [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    weakSelf.page = @"0";

    [weakSelf tableReloadCtrlWithPage];
}];

weakSelf.nomalTable.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
    NSInteger index = [self.page intValue];
    index += 1;

    weakSelf.page = [NSString stringWithFormat:@"%ld",(long)index];
    [weakSelf tableReloadCtrlWithPage];
}];

[weakSelf.nomalTable.mj_header beginRefreshing];
}



 // 這裡的page指的是分頁頁數
 // 當為if page == 1 else page += 1
  - (void)tableReloadCtrlWithPage {
    [self.nomalTable reloadData];

if (!self.dataArr.count?YES:NO) {
    self.nomalTable.tableFooterView = [self getTableFootView:self.nomalTable.bounds str:@"nodata"];
}else{
    self.nomalTable.tableFooterView = [UIView new];
}

NSInteger oldCnt = [self.dataArr count];
if (oldCnt > 0 && [self.page intValue] != 0) {
    NSInteger row = oldCnt - 20;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [self.nomalTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

}