UITableView 滑动自动加载更多数据

//
//  ViewController.m
//  loadMoreTableDemo
//
//  Created by zhangcheng on 15/11/7.
//  Copyright © 2013年 zhangcheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak)UITableView *tabelView;
@property(nonatomic,strong)NSMutableArray *arrM;
@end

@implementation ViewController

- (NSMutableArray *)arrM{
    if (_arrM == nil) {
        _arrM = [NSMutableArray arrayWithObjects:@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",@"A",nil];
    }
    return _arrM;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    self.tabelView = tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger count = self.arrM.count;
    
    return  count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID =@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
   
  cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
   
    NSInteger row = indexPath.row ;
    if ( row == self.arrM.count ) {
        cell.textLabel.text =@"查看更多";
    }else{
        cell.textLabel.text = [self.arrM objectAtIndex:indexPath.row];
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.arrM.count) {
        [self performSelectorInBackground:@selector(loadMore) withObject:nil];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
}

- (void)loadMore{
    NSMutableArray *more;
    more =  [NSMutableArray arrayWithObjects:@"最新数据", nil];
    [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
}

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

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值