封装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(100, 100, 100, 50) 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:70];

    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(70, 70);
    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(70, 70)];

    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 == 0) {
        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 == 0) {

        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

---恢复内容结束---

时间: 2024-10-19 01:59:09

封装Button ,封装UITableView,封装UICollectionView的相关文章

一种控制智能卡模块封装尺寸的UV封装设备

本实用新型公开了一种控制智能卡模块封装尺寸的UV封装设备,包括工作台和安装在所述工作台上方的滴胶装置和UV固化炉,所述UV固化炉位于所述滴胶装置的右边,所述UV封装设备还包括低温装置,所述低温装置设置在所述滴胶装置和UV固化炉之间,且靠近所述滴胶装置,所述低温装置为朝向下端开口的中空箱体,所述低温装置的左侧面的底部和右侧面的底部各设一缺口,所述低温装置的下端扣合在所述工作台上,所述低温装置的后端通过第一支架与所述工作台连接.本实用新型的控制智能卡模块封装尺寸的UV封装设备,利用UV胶的黏度随温度

SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装

SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ?Copyright 蕃薯耀 2017年7月6日 http://www.cnblogs.com/fanshuy

初学者如何新建一个封装的类来封装方法

第一步 AppDelegate中引用导航器,并设置根视图,同时隐藏导航栏 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for c

iOS中UITableView和UICollectionView的默认空态页

项目中想实现空态页风格统一控制的效果,就封装了一个默认空态页,使用的技术点有:1 方法替换 ,2 给分类(Category)添加属性. 我们知道,扩展(extension)可以给类添加私有变量和方法.但是分类(Category)不可以,但是我们又想在原生的UITableView和UICollectionView上添加空态页,所以使用了黑科技runtime中提供的对象关联.objc_setAssociatedObject/objc_getAssociatedObject. 懒得说明了,具体看代码.

iOS8自动调整UITableView和UICollectionView布局

本文讲述了UITableView.UICollectionView实现self-sizing cell布局的知识,以及如何用InvalidationContext优化UICollectionView布局的更新. 背景 iOS越来越人性化了,用户可以在设置-通用-辅助功能中动态调整字体大小了.你会发现所有iOS自带的APP的字体大小都变了,可惜我们开发的第三方APP依然是以前的字体.在iOS7之后我们可以用UIFont的preferredFontForTextStyle:类方法来指定一个样式,并让

RumTime实践之--UITableView和UICollectionView缺省页的实现

有关RunTime的知识点已经看过很久了,但是一直苦于在项目中没有好的机会进行实际运用,俗话说"光说不练假把式",正好最近在项目中碰到一个UITableView和UICollectionView在数据缺省的情况下展示默认缺省页的需求,这个时候RunTime大展拳脚的时候就到了. 大致的实现思路是这样的,因为UITableView和UICollectionView都是继承自系统的UIScrollView,所以为了同时实现UITableView和UICollectionView的缺省页,我

iOS 8自动调整UITableView和UICollectionView布局

本文讲述了UITableView.UICollectionView实现 self-sizing cell 布局的知识,以及如何用 InvalidationContext 优化 UICollectionView 布局的更新. 背景 iOS 越来越人性化了,用户可以在设置-通用-辅助功能中动态调 “” 阅读器 UITableViewUICollectionView (via:玉令天下的Blog) 本文讲述了UITableView.UICollectionView实现 self-sizing cell

IOS-给UIScrollView(包括继承它的UITableView、UICollectionView)添加下拉刷新-上拉加载更多

IOS里面用到的下拉刷新.上拉加载更多控件,开源的第三方框架很多,我们可以直接拿过来用,别人造好的轮子我们就没有必要再造一遍了,这里推荐几款下拉刷新.上拉加载更多控件 只有下拉刷新的: 一.EGOTableViewPullRefresh 这个使用起来比较麻烦,需要实现其各种协议,github地址为: https://github.com/enormego/EGOTableViewPullRefresh 这个貌似有个扩展上拉加载更多的,有时间补上 二.PullToRefresh  这个使用起来比E

[转]iOS8 自动调整UITableView和UICollectionView布局

转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog) ? 本文讲述了UITableView.UICollectionView实现 self-sizing cell 布局的知识,以及如何用 InvalidationContext 优化 UICollectionView 布局的更新. ? 背景 iOS 越来越人性化了,用户可以在设置-通用-辅助功能中动态调整字体大小了.你会发现所有 iOS 自带的APP的字体