上拉刷新后发现数据位置错乱?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];
}
}