当前位置: 首页 > news >正文

UITableView分页

UITableView分页上拉加载简单,ARC环境,源码如下,以作备份:

原理是,点击最后一个cell,触发一个事件来处理数据,然后reloadData

RootViewController.m + RootViewController.h

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView     *tableView;
@property (nonatomic, strong) NSMutableArray  *dataSource;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _dataSource = [[NSMutableArray alloc] init];
    for (int i = 0; i < 10; i++)
    {
        [_dataSource addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    _tableView  = [[UITableView alloc] initWithFrame:self.view.bounds
                                               style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 因为要定制最后一行,所以cell多一个
    return _dataSource.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedStr = @"demo";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:reusedStr];
    }
    
    if([indexPath row] == ([_dataSource count]))
    {
        // 定制最后一行的cell
        cell.textLabel.text=@"获取更多..";
    }
    else
    {
        // 定制普通行的cell
        cell.textLabel.text=[_dataSource objectAtIndex:[indexPath row]];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 如果是最后一行点击事件,则触发一个事件
    if (indexPath.row == ([_dataSource count]))
    {
        [self performSelectorInBackground:@selector(loadMore)
                               withObject:nil];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
}

- (void)loadMore
{
    // 添加数据源
    for (int i = 0; i < 10; i++) {
        [_dataSource addObject:[NSString stringWithFormat:@"新%d", i]];
    }
    
    // 重新加载tableView
    [_tableView reloadData];
}

@end


#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

相关文章:

  • 分布式集群环境下,如何实现每个服务的登陆认证?
  • 【中亦安图】Oracle内存过度消耗风险提醒(6)
  • 你知道JWT是什么吗?它和Session的区别又在哪里?
  • hadoop家族成员
  • 项目经理最近感觉系统慢了,想知道整个系统每个方法的执行时间
  • 获得指定文件夹所有文件的路径
  • 面试官问我:Zookeeper实现分布式锁的原理是什么?
  • typedef与#define的区别
  • 一步步教你如何在SpringBoot项目中引入支付功能
  • OSChina 周三乱弹 ——你是有多寂寞啊,看光头强都……
  • 今天不聊技术,谈谈我眼中的程序员到底是个怎样的职业
  • 关于JVM调优,我理了一些工具和思路出来
  • 2016年4月20日***学习总结
  • 关于ThreadLocal的九个知识点,看完别再说不懂了
  • Java程序员需要知道的操作系统知识汇总(持续更新)
  • 自己简单写的 事件订阅机制
  • 2019.2.20 c++ 知识梳理
  • HashMap剖析之内部结构
  • HTTP 简介
  • HTTP--网络协议分层,http历史(二)
  • Java 网络编程(2):UDP 的使用
  • JavaScript 奇技淫巧
  • Java编程基础24——递归练习
  • Lsb图片隐写
  • Lucene解析 - 基本概念
  • Mac转Windows的拯救指南
  • mysql常用命令汇总
  • Node 版本管理
  • Otto开发初探——微服务依赖管理新利器
  • Redis在Web项目中的应用与实践
  • Spring核心 Bean的高级装配
  • tab.js分享及浏览器兼容性问题汇总
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • 回顾2016
  • 基于 Babel 的 npm 包最小化设置
  • 看域名解析域名安全对SEO的影响
  • 前嗅ForeSpider中数据浏览界面介绍
  • 如何利用MongoDB打造TOP榜小程序
  • 线上 python http server profile 实践
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • Java性能优化之JVM GC(垃圾回收机制)
  • Spring Batch JSON 支持
  • ​​​​​​​​​​​​​​Γ函数
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (pojstep1.3.1)1017(构造法模拟)
  • (WSI分类)WSI分类文献小综述 2024
  • (二)JAVA使用POI操作excel
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功