在ios开中中,由于屏幕尺寸限制,如果需要显示的数据很多,需要用到分页加载。

原理:先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 比如:

数据源是个array:

NSMutableArray *items;

ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

int count = [items count];

return  count + 1;

}

这个方法定制cell的显示, 尤其是"加载更多"的那个cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if([indexPath row] == ([items count])) {

//创建loadMoreCell

return loadMoreCell;

}

//create your data cell

return cell;

}

还要处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == [items count]) {

[loadMoreCell setDisplayText:@"loading more ..."];

[loadMoreCell setAnimating:YES];

[self performSelectorInBackground:@selector(loadMore) withObject:nil];

//[loadMoreCell setHighlighted:NO];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

return;

}

//其他cell的事件

}

加载数据的方法:

-(void)loadMore

{

NSMutableArray *more;

//加载你的数据

[self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];

}

添加数据到列表:

-(void) appendTableWith:(NSMutableArray *)data

{

for (int i=0;i<[data count];i++) {

[items addObject:[data objectAtIndex:i]];

}

NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];

for (int ind = 0; ind < [data count]; ind++) {

NSIndexPath    *newPath =  [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];

[insertIndexPaths addObject:newPath];

}

[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];

}

我这里是写死的数据,作例子,在实现工程项目中,比如:要向服务器请求数据,一般返回数据会有总共多少页,一次请求一页,当用户需要看更多页面时,滑动到最下面cell,再请求下一面

我这里例子,请求加载更多是写到didSelectRowAtIndexPath:代理方法中,如果需要自动加载,可以放到- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

代理方法中。

tableView 加载更多的更多相关文章

  1. tableView中的“点击加载更多”点击不到

    假设当前的tableView是_tableView,则可以这样设置 _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0); 该属性用于设置当 ...

  2. IOS - UITableView分批显示数据 实现点击加载更多

    Phone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据.基本上就是数据源里先只放10条, 点击 ...

  3. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  4. iOS开发UI篇—在UItableview中实现加载更多功能

    一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据.                      二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...

  5. UITableView:下拉刷新和上拉加载更多

    [转载请注明出处] 本文将说明让UIScrollView支持"下拉刷新"和"上拉加载更多"的实现机制,并实现一个可用的tableView子类,以下主要以&quo ...

  6. UITableview优化随笔(1)-提高加载更多内容时的效率

    UITableView上拉加载更多的功能相信很多应用都会用到,类似朋友圈.微博这样的应用,tableView中的数据内容高度根据内容来变化,同时需要加载大量的数据(上拉加载更多),要怎样才能保证加载数 ...

  7. iOS:详解MJRefresh刷新加载更多数据的第三方库

    原文链接:http://www.ios122.com/2015/08/mjrefresh/ 简介 MJRefresh这个第三方库是李明杰老师的杰作,这个框架帮助我们程序员减轻了超级多的麻烦,节约了开发 ...

  8. 下拉刷新和上拉加载更多(第三方框架MJRefresh)

    #import "RootViewController.h" #import "MJRefresh.h" @interface RootViewControll ...

  9. iOS-tableView上拉加载更多后,界面出现偏移

    问题描述: 在做tableview的界面展示的时候,cell用自动计算高度的.但是在上拉加载更多的时候,数据请求完后,刷新界面,界面的顶部就出现了偏移 分析: 查阅资料后发现,当tableView的c ...

随机推荐

  1. FDMemTable的详细使用方法

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  2. Org-mode五分钟教程ZZZ

    Table of Contents 1 源起 2 简介 2.1 获取 org-mode 2.2 安装 3 基础用法 3.1 创建一个新文件 3.2 简单的任务列表 3.3 使用标题组织一篇文章 3.4 ...

  3. HNU 12834 Thread Tree

    递归输出即可了 #include<bits/stdc++.h> using namespace std; struct tree{     int dot;     string s; } ...

  4. VC++如何折叠代码

    工具-选项,然后在文本编辑器,C/C++中的格式设置,把大纲语句块设置为True   这样之后,还是不能像C#一样使用region折叠代码,但是可以方法和if语句都会自动显示可以折叠.   使用#pr ...

  5. long类型字段转换成varchar2类型

    參考文档: How to Convert a Long to Varchar2 (文档 ID 228532.1) /*long类型字段转换成varchar2类型*/ --建表 create table ...

  6. Linux —— 查找与替换

    Linux —— 查找与替换 文本查找: grep, egrep, fgrep        grep:根据基本正则表达式定义的模式搜索文档,并将符合模式的文本行显示出来        注意:搜索时属 ...

  7. Windows下安装MySQL5.6绿色版

    建议安装MySQL绿色版的,什么是绿色版的?就是免安装,下载下来的截图是这样的 在该目录下创建一个文件夹/data用于存放数据, 新建一个my.ini文件,my.ini里面最基本的配置如下,my.in ...

  8. 第3周课后实践&#183;程序阅读(4)-利用引用訪问私有数据成员

    /* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名:test.cpp * 作 者:刘畅 * 完毕日期:2015年 3 月 2 ...

  9. jquery源码学习笔记二:jQuery工厂

    笔记一里记录,jQuery的总体结构如下: (function( global, factory ) { //调用factory(工厂)生成jQuery实例 factory( global ); }( ...

  10. android.view.View

    * This class represents the basic building block for user interface components. A View * occupies a ...