#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

最新文章

  1. Javacript实现字典结构
  2. android camera 自定义开发
  3. git将代码提交到远程分支(非主分支)
  4. 微信小程序实例-获取当前的地理位置、速度
  5. ASP.NET MVC5利用EF,反向自动生成数据库
  6. Winform开发框架之客户关系管理系统(CRM)的开发总结系列1-界面功能展示
  7. 超酷的测速网站Ookla SPEEDTEST
  8. [Exchange]2个不同域之间互发邮件
  9. C++编程规范之19:总是初始化变量
  10. 0122——UITabBarController
  11. 【JSP引入报错】--package javax.servlet.jsp does not exist
  12. 微软的MCE(Media Center Edition 媒体中心)标准
  13. android细节之禁用activity的系统的默认切换效果
  14. 介绍4种HTML5 Canvas库
  15. Linux操作系统-命令-top
  16. BeautifulSoup练习第一节
  17. 【Spring】HttpMessageConverter的作用及替换
  18. HashMap和Hashtable的区别 2
  19. 7617:输出前k大的数
  20. golang cannot assign to

热门文章

  1. js闭包测试
  2. JS初识(着重讲解Date函数)
  3. 埃氏筛法(快速筛选n以内素数的个数)
  4. socket的异步回调函数,采用一问一答
  5. 结构类模式(五):外观(Facade)
  6. miniui datagrid 保存到服务端,使用.NET 自带 JSON 转换时发现日期格式不兼容。
  7. 全世界最短IE判定if(!+[1,])的解释(转)
  8. MFC打开文件对话框
  9. wpf图片切换,幻灯效果
  10. MFC非模态对话框中屏蔽ESC键