美文网首页iOS收藏Bug(代码)
MJRefresh不停上拉加载的问题

MJRefresh不停上拉加载的问题

作者: rankiLau | 来源:发表于2016-10-10 19:36 被阅读3062次

    下边这段代码就是出现不停上拉加载的代码,代码中注释掉了self.tableView.estimatedRowHeight=2.0f。

    以下为.m文件

    #import"HYBidConsumeRecordViewController.h"

    #import"MJRefresh.h"

    #import"HYTestModel.h"

    #import"testCell.h"

    staticNSString*TestCellIdentifier =@"testID";

    @interfaceHYBidConsumeRecordViewController()

    @property(nonatomic,strong)NSMutableArray*consumeRecordList;

    @property(nonatomic,assign)NSIntegerrequestCurrentPage;

    @property(nonatomic,strong)UITableView*tableView;

    @property(nonatomic,copy)NSString*timestamp;

    @end

    @implementationHYBidConsumeRecordViewController

    #pragma mark - Life Cycle

    - (void)viewDidLoad

    {

    [superviewDidLoad];

    self.title=@"消费记录";

    self.view.backgroundColor=HYRGBCOLOR(246,247,249);

    _consumeRecordList= [@[]mutableCopy];

    [selfinitTableView];

    }

    - (void)viewWillAppear:(BOOL)animated

    {

    [superviewWillAppear:animated];

    }

    -(void)viewWillDisappear:(BOOL)animated

    {

    [superviewWillDisappear:animated];

    }

    #pragma mark - Assist

    - (void)initTableView

    {

    self.tableView= [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];

    self.tableView.backgroundColor=HYRGBCOLOR(246,247,249);

    self.tableView.showsVerticalScrollIndicator=YES;

    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;

    self.tableView.delegate=self;

    self.tableView.dataSource=self;

    [selfaddRefresh];

    [self.viewaddSubview:self.tableView];

    //self.tableView.estimatedRowHeight=2.0f;

    }

    #pragma mark - UITableViewDelegate & UITableViewDataSource

    - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

    {

    return_consumeRecordList.count;

    }

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

    {

    testCell*cell = [tableViewdequeueReusableCellWithIdentifier:TestCellIdentifier];

    if(!cell)

    {

    cell = [[testCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:TestCellIdentifier];

    }

    HYTestModel*model =self.consumeRecordList[indexPath.row];

    cell.titleLabel.text= model.title;

    cell.Creattime.text= model.timeTitle;

    cell.timeContent.text= model.time;

    cell.Remark.text= model.contentTitle;

    cell.RemarkContent.text= model.content;

    cell.BusinessPrice.text= model.priceTitle;

    cell.Price.text= model.price;

    returncell;

    }

    - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

    {

    [tableViewdeselectRowAtIndexPath:indexPathanimated:YES];

    }

    #pragma mark - Request

    - (void)requestConsumeRecordList

    {

    ZBNetRequestData*req = [[ZBNetRequestDataalloc]init];

    [reqsetString:@"1"forKey:@"cityId"];

    [reqsetString:@""forKey:@"areaId"];

    [reqsetString:@""forKey:@"timestate"];

    [reqsetString:_timestampforKey:@"timestamp"];

    [ZBNetAPIClientGET:@"/appbatch/getBids"parameters:[reqtoDictionary]success:^(NSURLSessionDataTask*task,idresponseObject) {

    NSArray*tmpArray = responseObject[@"result"][@"data"];

    if(tmpArray && tmpArray.count!=0)

    {

    for(NSDictionary*dicintmpArray)

    {

    HYTestModel*testModel = [[HYTestModelalloc]initWithDictionary:dic];

    [_consumeRecordListaddObject:testModel];

    }

    if([_timestampisEqualToString:@""])

    {

    [self.tableView.headerendRefreshing];

    }

    else

    {

    [self.tableView.footerendRefreshing];

    }

    }

    else

    {

    }

    _timestamp=responseObject[@"result"][@"timestamp"];

    [self.tableViewreloadData];

    }failure:^(NSURLSessionDataTask*task,NSError*error) {

    NSLog(@"responseObject:%@",error);

    }];

    }

    #pragma mark - Refresh & Load

    - (void)addRefresh

    {

    MJRefreshNormalHeader*header = [MJRefreshNormalHeaderheaderWithRefreshingTarget:selfrefreshingAction:@selector(pullDownRefreshing)];

    [headersetTitle:@"下拉刷新"forState:MJRefreshStateIdle];

    [headersetTitle:@"松开刷新消费记录"forState:MJRefreshStatePulling];

    [headersetTitle:@"正在刷新消费记录..."forState:MJRefreshStateRefreshing];

    [headerbeginRefreshing];

    self.tableView.header= header;

    MJRefreshAutoNormalFooter*footer = [MJRefreshAutoNormalFooterfooterWithRefreshingTarget:selfrefreshingAction:@selector(loadMoreRefreshing)];

    [footersetTitle:MJRefreshAutoFooterIdleTextforState:MJRefreshStateIdle];

    [footersetTitle:@"正在加载更多记录..."forState:MJRefreshStateRefreshing];

    [footersetTitle:@""forState:MJRefreshStateNoMoreData];

    self.tableView.footer= footer;

    }

    - (void)pullDownRefreshing

    {

    [_consumeRecordListremoveAllObjects];

    self.timestamp=@"";

    [selfrequestConsumeRecordList];

    }

    - (void)loadMoreRefreshing

    {

    [selfrequestConsumeRecordList];

    }

    @end

    把self.tableView.estimatedRowHeight=2.0f注释打开,

    问题还存在,依旧不停进行上拉加载操作。

    解决方法:把self.tableView.estimatedRowHeight=150.0f;150这个数值是你cell的平均高度,如果estimatedRowHeight设置非常小,MJRefresh的KVO会监听错误的contentoffset,造成不停进行上拉加载操作。

    解决问题的过程:首先我反复的验证我代码逻辑,确保代码逻辑无误。之后我把重点代码打断点一行一行debug,因为毕竟眼睛验证过的逻辑容易出现漏洞,再确保没有循环调用上拉操作方法的情况下。我怀疑是工程中有其他类监听了contentoffset,于是看MJRefresh源码,KVO中监听了contentoffset contentsize refreshstate三个属性,监听方法打断点调试,发现每次调用上拉操作后,监听者又监听到contentoffset的变化,说明contentoffset还是处于变化当中,而estimatedRowHeight预估行高后算出一个contentoffset值,cell以实际大小布局后,contentoffset又会算出一个值,这样KVO就会监听到contentoffset的变化,就会去调用上拉操作的接口,加载出来的数据会去填充tableview,estimatedRowHeight会预估行高算出一个contentoffset,之后cell会已实际大小布局,contentoffset又会算出一个值,之后就循环开来。

    相关文章

      网友评论

      本文标题:MJRefresh不停上拉加载的问题

      本文链接:https://www.haomeiwen.com/subject/vbzmyttx.html