UITableView 滑动自动加载更多数据
2015-11-08 20:00
393 查看
// // 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
相关文章推荐
- Gson 解析 json中 value为数组
- Light OJ 1369 - Answering Queries 【规律】
- 快速排序
- Adreno Profiler GPU frequency clocks per/second
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F】【贪心+ STL-优先队列】 Gourmet and Banquet 最大活动时间
- UIGestureRecognizer(进阶)
- UIFont中fontName大全
- iOS之UILabel显示内容自动换行
- ios开发-UIImageView的使用,点击图片刷新对应数据的过程
- 设置IIS7文件上传的最大大小 maxAllowedContentLength,maxRequestLength
- 1369 - Answering Queries
- ios开发-UISlider通过滑动添加单元格功能
- java parseint 和 Integer.value
- Arduino小车学习与研究
- This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- mjpg-streamer在pcduino上的应用
- 百度编辑器ueditor获取不到内容?请把form放在table等其他元素最外面
- 关于UITableView的一点使用心得
- 什么是KVC?(What Is Key-Value Coding?)
- [leetcode-300]Longest Increasing Subsequence(java)