TableView的封装
1、基于NSObject封装TableView
TableViewDelegateObj.h
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h>typedef void (^selectCell) (NSIndexPath *indexPath); /*** 代理对象(UITableView的协议需要声明在.h文件中,不然外界在使用的时候会报黄色警告,看起来不太舒服)*/ @interface TableViewDelegateObj : NSObject <UITableViewDelegate, UITableViewDataSource>/*** 创建代理对象实例,并将数据列表传进去* 代理对象将消息传递出去,是通过block的方式向外传递消息的* @return 返回实例对象*/ + (instancetype)createTableViewDelegateWithDataList:(NSArray *)dataListselectBlock:(selectCell)selectBlock; @end
TableViewDelegateObj.m
#import "TableViewDelegateObj.h"@interface TableViewDelegateObj () @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, copy) selectCell selectBlock; @end@implementation TableViewDelegateObj+ (instancetype)createTableViewDelegateWithDataList:(NSMutableArray *)dataListselectBlock:(selectCell)selectBlock {return [[[self class] alloc] initTableViewDelegateWithDataList:dataListselectBlock:selectBlock]; }- (instancetype)initTableViewDelegateWithDataList:(NSMutableArray *)dataList selectBlock:(selectCell)selectBlock {self = [super init];if (self) {self.dataList = dataList;self.selectBlock = selectBlock;}return self; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *identifier = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];}cell.textLabel.text = self.dataList[indexPath.row];return cell; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.dataList.count; }- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[tableView deselectRowAtIndexPath:indexPath animated:NO];// 将点击事件通过block的方式传递出去self.selectBlock(indexPath); }
2、 Controller调用
#import "ViewController.h" #import "TableViewDelegateObj.h" @interface ViewController () @property (strong, nonatomic) NSMutableArray * arr_test; @property (strong, nonatomic) TableViewDelegateObj * tbd_obj; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.arr_test = [NSMutableArray arrayWithObjects:@"索马里猫",@"土耳其梵猫",@"美国短毛猫", nil];self.tbd_obj = [TableViewDelegateObj createTableViewDelegateWithDataList:self.arr_testselectBlock:^(NSIndexPath *indexPath) {NSLog(@"第%@行",indexPath);}];self.tb_test.delegate = self.tbd_obj;self.tb_test.dataSource = self.tbd_obj;//增加延时看下效果[self performSelector:@selector(reloadDataFromNewREquest) withObject:nil afterDelay:2.0];}- (void)reloadDataFromNewREquest {[self.arr_test addObject:@"非洲薮猫"];[self.tb_test reloadData]; }- (NSMutableArray *)arr_test {if (!_arr_test) {_arr_test = [[NSMutableArray alloc]init];}return _arr_test; }
参考地址:http://www.cocoachina.com/ios/20160317/15696.html
转载于:https://www.cnblogs.com/superbobo/p/5286469.html
TableView的封装相关推荐
- ios中tableview网封装(viewcontroller封装)常用的
下载地址 http://pan.baidu.com/share/link?shareid=3657500168&uk=923776187 使用框架 1:asIHttpRequest库 2;SB ...
- 封装scrollView 循环滚动,tableViewCell(连载) mvc
封装 封装 封装 ... 封装的重要性太重要了 给大家在送点干货 从一个项目中抽取出来的.和大家一起分享 封装scrollView 循环滚动.tableViewCell(连载) 明天还会更新 tabl ...
- Swift项目,超美的动画和tableView,collectionView,轮播图的使用,网络请求的封装等
<一>项目介绍: 这是一个Swift语言的项目,但是其中也有使用一些OC的三方库,比SDWebImage.WebViewJavascriptBridge等,同时实现了基本App的框架功能, ...
- 一劳永逸,iOS 自定义 ActionSheet 封装流程
原文链接:http://www.jianshu.com/p/cfb87a7db7b1 本文为 iOS 自定义视图封装<一劳永逸>系列的第四期,旨在提供封装思路,结果固然重要,但理解过程才最 ...
- KVO方式监听数组的变化动态刷新tableView
写作本文来由: iOS默认不支持对数组的KVO,因为普通方式监听的对象的地址的变化,而数组地址不变,而是里面的值发生了改变 整个过程需要三个步骤 (与普通监听一致) /* * 第一步 建立观察者 ...
- IOS中UITableview中封装九宫格
第一步引入SecondNav目录即可 第二步引入头文件 #import "DIYTableView.h" #import "invoiceInfo.h" 实现协 ...
- Alamofire源码解读系列(五)之结果封装(Result)
本篇讲解Result的封装 前言 有时候,我们会根据现实中的事物来对程序中的某个业务关系进行抽象,这句话很难理解.在Alamofire中,使用Response来描述请求后的结果.我们都知道Alamof ...
- IOS TableView的Cell高度自适应,UILabel自动换行适应
需求: 1.表格里的UILable要求自动换行 2.创建的tableViewCell的高度会自动适应内容的高度 一.用xcode构建项目,创建一个有tableView的视图,用纯代码的形式实现: 1. ...
- IOS开发之sqlite封装
上一节实现了最基本的增删改查,所有操作数据库的方法都写在控制器里,这样会有一个问题,如果修改CURD(增删改查)操作方法会非常麻烦,这一节我们对CURD进行封装,在控制器里直接调用封装好的工具. 下面 ...
最新文章
- 难忘的一天——装操作系统(四)
- 【从零开始的ROS四轴机械臂控制】(七)- ROS与arduino连接
- adb cannot connect to daemon_手机触屏失效的抢救办法,以及如何利用adb实现PC与手机交互...
- 利用 Numpy 进行矩阵相关运算
- Android项目架构设计深入浅出
- Ajax/JavaScript脚本大全,JS脚本大全
- 数据结构笔记(二) 栈和队列(C语言描述)
- Spring Boot Actuator [监控与管理]
- mysql中delete,truncate,drop区别
- 移动web开发之flex布局笔记
- Android动态加载字节码
- django为Form生成的label标签添加class
- python转义字符表
- [求助]关于服务器之间的文件拷贝问题,没有头绪,希望大家指点一二
- 分子模拟软件amber_薛定谔 autodock 分子动力学模拟GROMACS软件
- 大数据产品推荐:金蜂巢大数据集成与脱敏系统
- win10系统从旧固态迁移到新固态,win10系统迁移到固态硬盘ssd
- 使用Unity创建一个游戏场景
- 罗克韦尔AB PLC(RSLogix 5000)在线修改程序的具体方法示例
- git提交失败running pre-commit hook: lint-staged [33m[33m‼[33m Some of your tasks use `git add` command