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
UITableView加载显示更多内容的更多相关文章
- bootstrap模态框modal使用remote第二次加载显示相同内容解决办法
bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法 bootstrap的modal中,使用remote可以动态加载页面到modal-body中,并弹窗显示 ...
- bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法
bootstrap的modal中,使用remote可以动态加载页面到modal-body中,并弹窗显示 如果提供的是 URL,将利用 jQuery 的 load 方法从此 URL 地址加载要展示的内容 ...
- [ActionScript 3.0] AS3.0 动态加载显示内容
可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...
- 浏览器加载显示html页面内容的顺序
我们经常看到浏览器在加载某个页面时,部分内容先显示出来,又有些内容后显示.那么浏览器加载显示html究竟是按什么顺序进行的呢 其实浏览器加载显示html的顺序是按下面的顺序进行的:1.IE下载的顺序是 ...
- SDWebImage 加载显示 GIF 与性能问题
SDWebImage 加载显示 GIF 与性能问题 SDWebImage 4.0 之前,可以用 UIImageView 显示 GIF 图.如果 SDWebImage 4.0 还这么做,只会显示静态图. ...
- 一个页面从输入url到页面加载显示完成,中间都经历了什么
第一种解释: 一般会经历以下几个过程: 1.首先,在浏览器地址栏中输入url 2.浏览器先查看浏览器缓存-系统缓存-路由器缓存,如果缓存中有,会直接在屏幕中显示页面内容.若没有,则跳到第三步操作. 3 ...
- Android WebView 加载富文本内容
WebView加载数据的方式有两种: 1. webView.loadUrl(data);//加载url 2. webView.loadDataWithBaseURL(null,data, " ...
- SDWebImage 加载显示 WebP 与性能问题
SDWebImage 加载显示 WebP 与性能问题 本文包含自定义下载操作 SDWebImageDownloaderOperation 与编码器 SDWebImageCoder.SDWebImage ...
- UITableView加载网络数据的优化
UITableView加载网络数据的优化 效果 源码 https://github.com/YouXianMing/Animations // // TableViewLoadDataControll ...
随机推荐
- C++视频课程小结(3)
C++远征之封装篇(上) 章节介绍: 每章小结: 第一章:课程介绍. 按照惯例是章节的总介绍,内容明显多了很多(为了做作业我还要赶进度的说),主要说了:类和对象是本章的主角,然后还有很多配角,像数据成 ...
- POJ3345
http://poj.org/problem?id=3345 大意: 大意是说现在有n个城市来给你投票,你需要至少拿到m个城市的赞成票.想要获得第i个城市的赞成需要花费w[i],有个条件就是某些城市是 ...
- HDU3657Game(最大流)
这几天敲了几道最大流的问题,发现网络流真是模板算法啊.... 敲来敲去敲了几遍,基本每遍都敲得让人灰心,但同时也感受到了网络流的强大所在,这是我做网络流的第一题,,本以为看了一遍小白书的代码差不多理解 ...
- HDU 1394Minimum Inversion Number(线段树)
题目大意是说给你一个数组(N个),没戏可以将其首部的k(k<N)个元素移动至尾部,这样总共会形成N个序列 现在要求这n个序列中逆序对数最少的那一个序列有多少个逆序对 最初的确是没太多思路,就算知 ...
- HDU2948Geometry Darts(简单计算几何)
题目大意就是说两个人掷飞镖,飞镖在所给定的图形内就记一分,现在给定N个图形(圆.三角形和矩形),问每一次比赛(没人分别掷三次)谁赢. #include <map> #include < ...
- IIS6.0禁止用户下载txt文件
服务器win2003+IIS6.0,在IIS上限制下载.txt文件,方法比较简单,在IIS的应用程序扩展名映射中为资源扩展名指定一个错误的可执行文件即可. 打开记事本,直接将空内容保存为C:\WIND ...
- Windows 10 Edge浏览器、照片查看程序关闭“平滑滚动”
升级到10后,这两个常用软件的“平滑滚动”功能,个人感觉体验有点不好,特别是图片这个自带程序,看了几十张图后就有点头晕了,所以把它关闭为好: 控制面板\系统和安全\系统\高级系统设置\高级\性能\设置 ...
- 关于WebPlayer Sandbox的小节
不可以像其他build target一样读写I/O 不可以call一些private或者internal methord 只要在一个top level的domain下可以不需要xml dmain po ...
- Nginx系列~Nginx服务启动不了
Nginx服务有时起动不了了,原因是80端口为其它应用程序占用了,这时,我们需要查看是哪个程序占用了它,可能是IIS的某个站点,或者Tomat,Apache等,都有可能,所以,我们需要查看一下电脑80 ...
- javascript第三方组件
一.一个javascript文件上传组件.转载:http://www.cnblogs.com/fumj/archive/2012/12/07/2806673.html http://fineuploa ...