ios 加载更多

     当我们一次要返回很多的信息到设备屏幕上时,如果数据量过大,一次性的返回就可能会影响应用的性能,造成网络的堵塞,这个时候我们会应用到分页的方法。

分页模式分为主动的模式和被动的模式,主动的模式是在满足条件的时候,可以自动的发出并请求数据,请求完成以后,会隐藏掉活动的指示器。

     被动的请求,需要我们创建一个响应的点击事件的控件,一般为按钮,当点击按钮的时候会向服务器发出请求,请求完成以后,隐藏掉按钮。

具体的加载更多如下:

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

   

    if (indexPath.row == [items count]) { 
        UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath]; 
        loadMoreCell.textLabel.text=@"loading more …"; 
        [self performSelectorInBackground:@selector(loadMore) withObject:nil]; 
       [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
        return; 
    } 
    //其他cell的事件 
    

-(void)loadMore 

    NSMutableArray *more; 
    more=[[NSMutableArray alloc] initWithCapacity:0]; 
    for (int i=0; i<10; i++) { 
        [more addObject:[NSString stringWithFormat:@"cell ++%i",i]]; 
    } 
    //加载你的数据 
    [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO]; 
    [more release]; 

-(void) appendTableWith:(NSMutableArray *)data 

    for (int i=0;i<[data count];i++) { 
        [items addObject:[data objectAtIndex:i]]; 
    } 
    NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10]; 
    for (int ind = 0; ind < [data count]; ind++) { 
        NSIndexPath    *newPath =  [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0]; 
        [insertIndexPaths addObject:newPath]; 
    } 
   [self.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值