#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

UITableView加载显示更多内容的更多相关文章

  1. bootstrap模态框modal使用remote第二次加载显示相同内容解决办法

    bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法 bootstrap的modal中,使用remote可以动态加载页面到modal-body中,并弹窗显示 ...

  2. bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法

    bootstrap的modal中,使用remote可以动态加载页面到modal-body中,并弹窗显示 如果提供的是 URL,将利用 jQuery 的 load 方法从此 URL 地址加载要展示的内容 ...

  3. [ActionScript 3.0] AS3.0 动态加载显示内容

    可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...

  4. 浏览器加载显示html页面内容的顺序

    我们经常看到浏览器在加载某个页面时,部分内容先显示出来,又有些内容后显示.那么浏览器加载显示html究竟是按什么顺序进行的呢 其实浏览器加载显示html的顺序是按下面的顺序进行的:1.IE下载的顺序是 ...

  5. SDWebImage 加载显示 GIF 与性能问题

    SDWebImage 加载显示 GIF 与性能问题 SDWebImage 4.0 之前,可以用 UIImageView 显示 GIF 图.如果 SDWebImage 4.0 还这么做,只会显示静态图. ...

  6. 一个页面从输入url到页面加载显示完成,中间都经历了什么

    第一种解释: 一般会经历以下几个过程: 1.首先,在浏览器地址栏中输入url 2.浏览器先查看浏览器缓存-系统缓存-路由器缓存,如果缓存中有,会直接在屏幕中显示页面内容.若没有,则跳到第三步操作. 3 ...

  7. Android WebView 加载富文本内容

    WebView加载数据的方式有两种: 1. webView.loadUrl(data);//加载url 2. webView.loadDataWithBaseURL(null,data, " ...

  8. SDWebImage 加载显示 WebP 与性能问题

    SDWebImage 加载显示 WebP 与性能问题 本文包含自定义下载操作 SDWebImageDownloaderOperation 与编码器 SDWebImageCoder.SDWebImage ...

  9. UITableView加载网络数据的优化

    UITableView加载网络数据的优化 效果 源码 https://github.com/YouXianMing/Animations // // TableViewLoadDataControll ...

随机推荐

  1. log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.

    The reason for the error is a new listener in Tomcat 6.0.24. You can fix this error by adding this l ...

  2. 异步编程之Promise(3):拓展进阶

    异步编程系列教程: (翻译)异步编程之Promise(1)--初见魅力 异步编程之Promise(2):探究原理 异步编程之Promise(3):拓展进阶 异步编程之Generator(1)--领略魅 ...

  3. js闭包测试

    本文的诞生,源自近期打算做的一个关于javascript中的闭包的专题,由于需要解析闭包对垃圾回收的影响,特此针对不同的javascript引擎,做了相关的测试. 为了能从本文中得到需要的知识,看本文 ...

  4. C++11空指针

    [C++11空指针] 早在 1972 年,C语言诞生的初期,常数 0 带有常数及空指针的双重身分. C 使用 preprocessor macro NULL 表示空指针, 让 NULL 及 0 分别代 ...

  5. Python基础 字符串的魔法

    capitalize(self) 返回值:将字符串的第一个首字母变成大写,其他字母变小写 s = 'hello World' ss = s.capitalize() print(ss) Hello w ...

  6. HDU 4497 GCD and LCM (数学,质数分解)

    题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...

  7. 解决sqlserver使用IP无法连接的问题,用localhost或者‘“.”可以连接

    今天装了一个mssql发现用ip无法连接但是用localhost和“.”却可以连接,纠结了一天终于找到了问题的解决办法: 打开mssql配置管理器(我点电脑---->右键选择管理--->服 ...

  8. 面试过程中javascript原型链与作用域的问题

    现在校招也基本结束了,所以有时间把这段时间遇到的问题做个总结.在很多的笔试题目中,发现有很多对JS作用域方面的考察,所以查阅资料总结一下. 众所周知,js不像其他OOP语言那样,他是一种弱类型的语言, ...

  9. Chrome developer tool:本人钟爱的 console、Network 功能简谈

    在最开始时,本人调试查看网页,一直用的是 firefox 的 firebug 插件,并没有使用 chrome 的 developer tool .只不过,在日常生活使用过程中,一直使用的是 chrom ...

  10. spring4 定时任务

    一.spring 定时任务 spring 定时任务 ,最好使用quartz 实现.下面我以spring4为例 二.实战(默认) 1.pom配置 <!-- spring time task --& ...