//
// 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
UITableView 滑动自动加载更多数据
最新推荐文章于 2020-12-21 21:25:11 发布