UITableView告别蛋疼的上拉更新,自动刷新数据。

来源:互联网 发布:淘宝pc转换无线连接 编辑:程序博客网 时间:2024/06/06 08:36

前语:上更新确实是个好东西,但是本人不喜欢。因为当我阅读完当前的数据的时候,还要上拉一下,等待刷新,这个对浏览信息来说是一个很蛋疼的事情。

下面介绍一下很简单的一个自动刷新的方法:(很简单的哦~!)

在delegate 里面

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    

    NSInteger row = [indexPathrow];

    if (_userRequestDataNumber<self.optionNumber) {      //首先判断是否还有可以刷新的数据

        if (row==(numberOfCell-5)&&[_requestisFinished]) {  //如果当用户滑动到倒数第五个的时候,而且当前请求已经结束的时候,去加载更多的数据。

            [selfloadMoreData]; //获取更多数据方法、

        }

    }

}

当然要处理一下tableview最后一个cell的效果,

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

{

    NSInteger count =self.bookTitleArray.count; //总的cell个数、

    if (count ==0) {

        return0;

    }

    if (_userRequestDataNumber <self.optionNumber) {  //如果还有可刷新的数据、返回cell的个数是 多一个。(包含tableview最后的那个刷新cell).

        return count +1;

    }

    return count;

}


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

{

    NSInteger row = [indexPathrow];

    

    if(row == _bookTitleArray.count) {

        staticNSString *MoreCellIdentifier =@"TableMoreCell";

        TableMoreCell *moreCell = [tableViewdequeueReusableCellWithIdentifier:MoreCellIdentifier];

        if (!moreCell) {

            NSArray *nib = [[NSBundlemainBundle]loadNibNamed:@"TableMoreCell"owner:selfoptions:nil];

            moreCell = [nib objectAtIndex:0];

        }

        if (![_requestisFinished]) {  //当前请求还没有结束的话显示小齿轮

/*-------加载小齿轮--------*./

            moreCellActivityIndicatorView = [[UIActivityIndicatorViewalloc]initWithFrame:CGRectMake(70,12,20,20)];

            moreCellActivityIndicatorView.activityIndicatorViewStyle =UIActivityIndicatorViewStyleGray;

            moreCellActivityIndicatorView.hidesWhenStopped =YES;

            [moreCellActivityIndicatorViewstartAnimating];

            [moreCelladdSubview:moreCellActivityIndicatorView];

            

            moreCell.stateLabel.text =@"正在加载";

        }else{

            moreCell.stateLabel.text =@"点击查看更多";

        }

        return moreCell;

    } else {...........//正常cell的样式处理}

}


效果图:如果滑动的慢,有刷新的数据,网速快的情况下,用户一直都不用手动去刷新,等到他还没有拉到最低端的时候,已经加载出了新的数据。




如果有人想等到用户拉到最低端在进行加载数据的话:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if(tableView.contentOffset.y + (tableView.frame.size.height) > tableView.contentSize.height){

//method

}

}


0 0