封装Button ,封装UITableView,封装UICollectionView
---恢复内容开始---
封装Button ,封装UITableView,封装UICollectionView:
1.实现Button的创建和点击事件不用分开操作处理;
2.实现UITableView的代理数据源方法不用分开操作;
3.实现UICollectionView的代理数据源方法不用分开操作;
实现如下 :
Button的实现 :
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
ZH_Button *button = [[ZH_Button alloc]initWithFrame:CGRectMake(, , , ) normalImageString:nil highlightImageString:nil];
[button setTitle:@"哈哈" forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[button addActionforControlEvents:UIControlEventTouchUpInside respond:^{
NSLog(@"我被点了");
}];
[self.view addSubview:button];
}
UITableView的实现:
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
[self createTableView];
}
- (void)loadData{
NSArray *dataArray = @[@"思思1",@"思思2",@"思思3",@"思思4",@"思思5",@"思思6",@"思思7",@"思思8",@"思思9",@"思思10",@"思思11"];
self.dataSources = [dataArray mutableCopy];
}
- (void)createTableView{
_myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.myTableView];
// 注册cell
[_myTableView registerClass:[MyCell class] forCellReuseIdentifier:identifier];
self.dataArray = [[ArrayDataSource alloc]initWithItems:self.dataSources cellIdentifier:identifier configureCellBlock:^(MyCell *cell, id item) {
cell.title = item;
} itemHeight:];
self.myTableView.dataSource = self.dataArray;
self.myTableView.delegate = self.dataArray;
__weak typeof(self)weakSelf = self;
self.dataArray.didSelect = ^ (NSIndexPath *indexPath){
NSString *title = weakSelf.dataSources[indexPath.row];
NSLog(@"选择的行:indexPath = %@,标题为 : %@",indexPath,title);
};
self.dataArray.didDeselectRow = ^ (NSIndexPath *indexPath){
NSLog(@"取消选择行: %@",indexPath);
};
self.dataArray.didHighlight = ^ (NSIndexPath *indexPath){
NSLog(@"高亮行: %@",indexPath);
};
}
UICollectionView的实现 :
- (void)viewDidLoad{
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
[super viewDidLoad];
[self loadData];
[self createCollectionView];
}
#pragma mark - createCollectionView
- (void)createCollectionView{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake(, );
self.myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
self.myCollectionView.backgroundColor = [UIColor clearColor];
// 注册cell
[self.myCollectionView registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:identifier];
// 设置数据源,代理
self.dataSourceArray = [[CollectionViewDataSource alloc]initWithItems:self.dataArray cellIdentifier:identifier configureCellBlock:^(MyCollectionCell *cell, id item) {
cell.title = item;
} itemSize:CGSizeMake(, )];
self.myCollectionView.dataSource = self.dataSourceArray;
self.myCollectionView.delegate = self.dataSourceArray;
[self.view addSubview:self.myCollectionView];
__weak typeof(self) weakSelf = self;
self.dataSourceArray.didSelect = ^ (NSIndexPath *indexPath){
NSString *title = weakSelf.dataArray[indexPath.row];
NSLog(@"选择的item:indexPath = %@,标题为 : %@",indexPath,title);
};
self.dataSourceArray.didDeselectRow = ^ (NSIndexPath *indexPath){
NSLog(@"取消选择item: %@",indexPath);
};
self.dataSourceArray.didHighlight = ^ (NSIndexPath *indexPath){
NSLog(@"高亮item: %@",indexPath);
};
}
- (void)loadData{
NSArray *dataArray = @[@"思思1",@"思思2",@"思思3",@"思思4",@"思思5",@"思思6",@"思思7",@"思思8",@"思思9",@"思思10",@"思思11"];
self.dataArray = dataArray;
}
具体的中间的封装:
1.Button的封装:
typedef void(^DGCompletionHandler)(void);
#import <UIKit/UIKit.h> @interface ZH_Button : UIButton // 按钮触发
- (void)addActionforControlEvents:(UIControlEvents)controlEvents respond:(DGCompletionHandler)completion; // 初始化
-(instancetype)initWithFrame:(CGRect)frame normalImageString:(NSString *)normalImageString highlightImageString:(NSString *)highlightImageString;
#import "ZH_Button.h"
#import <objc/runtime.h>
static void *BuClickKey = @"BuClickKey";
@implementation ZH_Button
-(instancetype)initWithFrame:(CGRect)frame normalImageString:(NSString *)normalImageString highlightImageString:(NSString *)highlightImageString{
if (self = [super initWithFrame:frame]) {
self = [ZH_Button buttonWithType:UIButtonTypeCustom];
self.frame = frame;
if (normalImageString) {
[self setImage:[UIImage imageNamed:normalImageString] forState:UIControlStateNormal];
}
if (highlightImageString) {
[self setImage:[UIImage imageNamed:highlightImageString] forState:UIControlStateHighlighted];
} }
return self;
}
- (void)addActionforControlEvents:(UIControlEvents)controlEvents respond:(DGCompletionHandler)completion{ [self addTarget:self action:@selector(didClickBU) forControlEvents:controlEvents]; void (^block)(void) = ^{ completion(); }; objc_setAssociatedObject(self, BuClickKey, block, OBJC_ASSOCIATION_COPY);
} -(void)didClickBU{
void (^block)(void) = objc_getAssociatedObject(self, BuClickKey);
block();
}
2.UITableView的封装:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> // 导入ArrayDataSource记得要导入UIKit框架,否则可能报错哟!!! typedef void (^shouldHighlight)(NSIndexPath*);
typedef void (^didSelect)(NSIndexPath *indexPath);
typedef void (^didHighlight)(NSIndexPath *indexPath);
typedef void (^didDeselectRow)(NSIndexPath *indexPath);
typedef void (^TableViewCellConfigureBlock)(id cell, id item); @interface ArrayDataSource : NSObject <UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,copy)didSelect didSelect;
@property(nonatomic,copy)didHighlight didHighlight;
@property(nonatomic,strong)shouldHighlight shouldHighlight;
@property(nonatomic,strong)didDeselectRow didDeselectRow; - (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
itemHeight:(float)itemHeight;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath; @end
#import "ArrayDataSource.h" @interface ArrayDataSource () @property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property(nonatomic, assign)float itemHeight; @property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock; @end @implementation ArrayDataSource #pragma mark - Init - (id)init
{
return nil;
} - (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock itemHeight:(float)itemHeight
{
self = [super init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConfigureCellBlock copy];
self.itemHeight = itemHeight;
}
return self;
} - (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger) indexPath.row];
} #pragma mark UITableView Delegate -(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.didHighlight) {
self.didHighlight(indexPath);
} }
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.shouldHighlight) {
self.shouldHighlight(indexPath);
} return YES;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.itemHeight;
} -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.didDeselectRow) {
self.didDeselectRow(indexPath);
} } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.didSelect) {
self.didSelect(indexPath);
}
} #pragma mark - UITableView DataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.items.count == ) {
NSLog(@"什么鬼");
}
return self.items.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
}
id item = [self itemAtIndexPath:indexPath];
if (self.configureCellBlock) {
self.configureCellBlock(cell, item);
}
return cell;
}
3.UICollectionView的封装:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> /** 数据源,代理回调block*/ typedef void (^shouldHighlight)(NSIndexPath*);
typedef void (^didSelect)(NSIndexPath *indexPath);
typedef void (^didHighlight)(NSIndexPath *indexPath);
typedef void (^didDeselectRow)(NSIndexPath *indexPath);
typedef void (^CollectionViewCellConfigureBlock)(id cell, id item); @interface CollectionViewDataSource : NSObject<UICollectionViewDelegate,UICollectionViewDataSource> @property(nonatomic,copy)didSelect didSelect;
@property(nonatomic,copy)didHighlight didHighlight;
@property(nonatomic,strong)shouldHighlight shouldHighlight;
@property(nonatomic,strong)didDeselectRow didDeselectRow; // 初始化方法
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(CollectionViewCellConfigureBlock)aConfigureCellBlock
itemSize:(CGSize)itemSize; // 根据indexPath取得对应的item
- (id)itemAtIndexPath:(NSIndexPath *)indexPath; @end
#import "CollectionViewDataSource.h" @interface CollectionViewDataSource () @property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property(nonatomic,assign)CGSize itemSize; @property (nonatomic, copy) CollectionViewCellConfigureBlock configureCellBlock; @end @implementation CollectionViewDataSource #pragma mark - Init - (instancetype)init{ return nil;
} - (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(CollectionViewCellConfigureBlock)aConfigureCellBlock
itemSize:(CGSize)itemSize
{
self = [super init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConfigureCellBlock copy];
self.itemSize = itemSize;
}
return self;
} - (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger) indexPath.row];
} #pragma mark - UICollectionView DataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if (self.items.count == ) { NSLog(@"数组为空啦!!!");
}
return self.items.count;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UICollectionViewCell alloc]init];
}
id item = [self itemAtIndexPath:indexPath];
if (self.configureCellBlock) {
self.configureCellBlock(cell,item);
}
return cell; }
#pragma mark - UICollectionView Delegate - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.didDeselectRow) {
self.didDeselectRow(indexPath);
}
} - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.didSelect) {
self.didSelect(indexPath);
}
} - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.didHighlight) {
self.didHighlight(indexPath);
}
} //定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.itemSize;
} @end
---恢复内容结束---
封装Button ,封装UITableView,封装UICollectionView的更多相关文章
- 用UIControl封装Button
用UIControl封装Button 效果 说明 UIControl在处理超出触摸范围的触摸事件时有bug 源码 基础类 // // BaseControl.h // BaseControl // / ...
- [分享] 史上最简单的封装教程,五分钟学会封装系统(以封装Windows 7为例)
[分享] 史上最简单的封装教程,五分钟学会封装系统(以封装Windows 7为例) 踏雁寻花 发表于 2015-8-23 23:31:28 https://www.itsk.com/thread-35 ...
- SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装
SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装 >>>>>>>& ...
- Spring NamedParameterJdbcTemplate命名参数查询条件封装, NamedParameterJdbcTemplate查询封装
Spring NamedParameterJdbcTemplate命名参数查询条件封装, NamedParameterJdbcTemplate查询封装 >>>>>> ...
- FFmpeg封装格式处理4-转封装例程
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10506662.html FFmpeg封装格式处理相关内容分为如下几篇文章: [1]. F ...
- 面向对象 - 1.封装之如何实现属性的隐藏/2.封装的意义/3.封装与扩展性/4.property的使用
1.封装之如何实现属性的隐藏封装: __x=1 # 把数据属性隐藏 (如何实现隐藏) 类定义阶段 __开头发生了变形 __x --> _A__x特点: 1.在类外部无法直接:obj.__Attr ...
- 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 02 封装的代码实现
088 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 02 封装的代码实现 本文知识点:Java封装的代码实现 说明:因为时间紧张,本人写博客过程中只 ...
- 087 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 01 封装的概念和特点
087 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 01 封装的概念和特点 本文知识点:封装的概念和特点 说明:因为时间紧张,本人写博客过程中只是对 ...
- 面向对象编程(封装、封装的意义、封装与扩展性、@property)
1.封装之如何实现属性的隐藏 封装: __x=1 # 把数据属性隐藏 (如何实现隐藏) 类定义阶段 __开头发生了变形 __x --> _A__x特点: 1.在类外部无法直接:obj.__Att ...
随机推荐
- 字符串搜索(strStr)--- C++版
上篇中是用JAVA实现的字符串搜索算法, 这次改用C++来实现,当然在C++就没有像JAVA那样方便的API可以很简便的实现了,其思想跟上篇类似,直接上具体实现代码: 编译运行: 下面分析下流程: 还 ...
- celery+django+mq 异步任务与定时任务
参考 celerypip install celery==4.1.1https://www.cnblogs.com/wdliu/p/9530219.htmlhttps://www.jianshu.co ...
- Selenium&Appium四种等待方式
一.摘要 本博文主要介绍自动化测试中,无论是selenium或是Appium的四种等待方式,合理的使用等待对代码的稳定性,测试效率都有很大的提高 隐式等待:是在尝试发现某个元素的时候,如果没能立刻发现 ...
- redis cluster 介绍
介绍 1. cluster的作用 (1)自动将数据进行分片,每个master上放一部分数据 (2)提供内置的高可用支持,部分master不可用时,还是可以继续工作的 2. redis集群实现方案 关于 ...
- 模块化开发之Amd规范和Cmd规范
CMD规范:是SeaJS 在推广过程中对模块定义的规范化产出的. AMD规范:是 RequireJS 在推广过程中对模块定义的规范化产出的 // CMD define(function(require ...
- java双指针的简单理解
一.什么是双指针 双指针我所理解地是在遍历对象时,不是使用单个指针进行访问,而是使用两个相同方向或者相反方向的指针进行遍历,从而达到相应的目的. 在JAVA中并没有像C/C++指针地概念,所以这里所说 ...
- CSRF(跨站请求伪造)
跨站请求伪造(Cross-site request forgery)是一种冒充受信任用户,向服务器发送非预期请求的攻击方式. 攻击的前提是: 用户已经登录过某网站. 攻击者通过一些诱惑性的标题,诱惑用 ...
- [Luogu] 国王游戏
https://www.luogu.org/problemnew/show/P1080 按照 a * b 排序 高精度 #include <bits/stdc++.h> using nam ...
- python2.X与Python3.X区别
__future__模块 [回到目录] Python 3.x引入了一些与Python 2不兼容的关键字和特性,在Python 2中,可以通过内置的__future__模块导入这些新内容.如果你希望在P ...
- UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)
先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...