UITableView加载显示更多内容
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *dataArray;//uitableview要显示数据
NSMutableArray *moreArray;//加载更多要显示的数据
}
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end
@implementation ViewController
#pragma mark - life circle
- (void)viewDidLoad
{
[super viewDidLoad];
[self addObjectToArray];
//这里不需要设置代理,因为已经在storyboard上进行关联
//_myTableView.dataSource=self;
//_myTableView.delegate=self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -UITableViewDataSource
//返回每个setion对应的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataArray.count+1;
}
//返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//返回cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(indexPath.row==dataArray.count)
{
cell.textLabel.text=@"加载更多more";
}
else
{
cell.textLabel.text=dataArray[indexPath.row];
}
return cell;
}
#pragma mark -UITableViewDataDelegate
//选择行数中的每行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==dataArray.count)
{
// UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath];
//loadMoreCell.textLabel.text=@"加载更多more";
//运用gcd多线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//后台线程
[self loadMore];
dispatch_async(dispatch_get_main_queue(), ^{
//主线程
[self appendTableWith:moreArray];
});
});
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
}
#pragma mark - private
//重新getter方法
-(NSMutableArray *)dataArray
{
if (dataArray==nil)
{
dataArray=[NSMutableArray array];
}
return dataArray;
}
//刚开始添加10条记录
-(void)addObjectToArray
{
for (int i=0; i<10; i++)
{
[self.dataArray addObject:[NSString stringWithFormat:@"cell %i",i]];
}
}
-(void)loadMore
{
//点击加载更多行 要追加显示的记录数
moreArray=[NSMutableArray array];
for (int i=0; i<10; i++)
{
[moreArray addObject:[NSString stringWithFormat:@"cell ++%i",i]];
}
}
//往tableview里面追加数据
-(void)appendTableWith:(NSMutableArray *)data
{
for (int i=0; i<data.count; i++)
{
//在原有数据的基础上再追加数据
[dataArray addObject:[data objectAtIndex:i]];
}
//把要追加显示的数据插入到指定cell的行中去
NSMutableArray *insertIndexPaths=[NSMutableArray array];
for (int j=0; j<data.count; j++)
{
NSIndexPath *newPath=[NSIndexPath indexPathForRow:[dataArray indexOfObject:[data objectAtIndex:j]] inSection:0];
[insertIndexPaths addObject:newPath];
}
[self.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
@end
最新文章
- Javacript实现字典结构
- android camera 自定义开发
- git将代码提交到远程分支(非主分支)
- 微信小程序实例-获取当前的地理位置、速度
- ASP.NET MVC5利用EF,反向自动生成数据库
- Winform开发框架之客户关系管理系统(CRM)的开发总结系列1-界面功能展示
- 超酷的测速网站Ookla SPEEDTEST
- [Exchange]2个不同域之间互发邮件
- C++编程规范之19:总是初始化变量
- 0122——UITabBarController
- 【JSP引入报错】--package javax.servlet.jsp does not exist
- 微软的MCE(Media Center Edition 媒体中心)标准
- android细节之禁用activity的系统的默认切换效果
- 介绍4种HTML5 Canvas库
- Linux操作系统-命令-top
- BeautifulSoup练习第一节
- 【Spring】HttpMessageConverter的作用及替换
- HashMap和Hashtable的区别 2
- 7617:输出前k大的数
- golang cannot assign to