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的封装相关推荐

  1. ios中tableview网封装(viewcontroller封装)常用的

    下载地址 http://pan.baidu.com/share/link?shareid=3657500168&uk=923776187 使用框架 1:asIHttpRequest库 2;SB ...

  2. 封装scrollView 循环滚动,tableViewCell(连载) mvc

    封装 封装 封装 ... 封装的重要性太重要了 给大家在送点干货 从一个项目中抽取出来的.和大家一起分享 封装scrollView 循环滚动.tableViewCell(连载) 明天还会更新 tabl ...

  3. Swift项目,超美的动画和tableView,collectionView,轮播图的使用,网络请求的封装等

    <一>项目介绍: 这是一个Swift语言的项目,但是其中也有使用一些OC的三方库,比SDWebImage.WebViewJavascriptBridge等,同时实现了基本App的框架功能, ...

  4. 一劳永逸,iOS 自定义 ActionSheet 封装流程

    原文链接:http://www.jianshu.com/p/cfb87a7db7b1 本文为 iOS 自定义视图封装<一劳永逸>系列的第四期,旨在提供封装思路,结果固然重要,但理解过程才最 ...

  5. KVO方式监听数组的变化动态刷新tableView

    写作本文来由:   iOS默认不支持对数组的KVO,因为普通方式监听的对象的地址的变化,而数组地址不变,而是里面的值发生了改变 整个过程需要三个步骤 (与普通监听一致) /* *  第一步 建立观察者 ...

  6. IOS中UITableview中封装九宫格

    第一步引入SecondNav目录即可 第二步引入头文件 #import "DIYTableView.h" #import "invoiceInfo.h" 实现协 ...

  7. Alamofire源码解读系列(五)之结果封装(Result)

    本篇讲解Result的封装 前言 有时候,我们会根据现实中的事物来对程序中的某个业务关系进行抽象,这句话很难理解.在Alamofire中,使用Response来描述请求后的结果.我们都知道Alamof ...

  8. IOS TableView的Cell高度自适应,UILabel自动换行适应

    需求: 1.表格里的UILable要求自动换行 2.创建的tableViewCell的高度会自动适应内容的高度 一.用xcode构建项目,创建一个有tableView的视图,用纯代码的形式实现: 1. ...

  9. IOS开发之sqlite封装

    上一节实现了最基本的增删改查,所有操作数据库的方法都写在控制器里,这样会有一个问题,如果修改CURD(增删改查)操作方法会非常麻烦,这一节我们对CURD进行封装,在控制器里直接调用封装好的工具. 下面 ...

最新文章

  1. 难忘的一天——装操作系统(四)
  2. 【从零开始的ROS四轴机械臂控制】(七)- ROS与arduino连接
  3. adb cannot connect to daemon_手机触屏失效的抢救办法,以及如何利用adb实现PC与手机交互...
  4. 利用 Numpy 进行矩阵相关运算
  5. Android项目架构设计深入浅出
  6. Ajax/JavaScript脚本大全,JS脚本大全
  7. 数据结构笔记(二) 栈和队列(C语言描述)
  8. Spring Boot Actuator [监控与管理]
  9. mysql中delete,truncate,drop区别
  10. 移动web开发之flex布局笔记
  11. Android动态加载字节码
  12. django为Form生成的label标签添加class
  13. python转义字符表
  14. [求助]关于服务器之间的文件拷贝问题,没有头绪,希望大家指点一二
  15. 分子模拟软件amber_薛定谔 autodock 分子动力学模拟GROMACS软件
  16. 大数据产品推荐:金蜂巢大数据集成与脱敏系统
  17. win10系统从旧固态迁移到新固态,win10系统迁移到固态硬盘ssd
  18. 使用Unity创建一个游戏场景
  19. 罗克韦尔AB PLC(RSLogix 5000)在线修改程序的具体方法示例
  20. git提交失败running pre-commit hook: lint-staged [33m‼ Some of your tasks use `git add` command

热门文章

  1. JAVA动态表单,自定义表单,自定义字段
  2. grep命令详细讲解
  3. 上善若水——项目管理的真谛
  4. Serialized class com.xxx.xxxService must implement java.io.Serializable
  5. MySQL - 完整性约束条件
  6. MATLAB实现粒子群算法的进阶讲解(多维+约束条件)
  7. python打印变量名
  8. 【计算机毕业设计】网上购物商城
  9. 纸黄金开户完全指南(最全最新)
  10. gather torch_Pytorch的gather用法理解