`
ilikeido
  • 浏览: 27443 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

BeeFramework 系列二 UISignal篇下

阅读更多
      上篇,我们讲了UISignal的工作原理,以及BeeUIButton中的一些用法,实际上,BeeFramework框架为大部分常用组件封装了UISignal,在应用中只需要对Signal进行处理就好了,这在一定程度上减轻了代码量。

      在实际应用中UITableView的场景可谓是无处不在,下面的例子实现了一个UITableViewCell的自定义UISignal。先看下效果图
     
      点击浏览或评论触发相应事件,为了响应这样的事件,通常的做法是在UITableViewCell中采用代理的方式,在ViewController中实现Cell的协议。
      下面看下Bee的写法
//
//  ViewController.h
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-3.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <BeeFramework/Bee.h>

@interface MyCell : UITableViewCell{
    UILabel *lb_content;
    UIButton *btn_count;
    UIButton *btn_comment;
}

@property(nonatomic,retain) NSDictionary *data;

AS_SIGNAL(COUNT)

AS_SIGNAL(COMMENT)

@end

@interface ViewController : UITableViewController

@end

//
//  ViewController.m
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-3.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "ViewController.h"

@implementation MyCell

DEF_SIGNAL(COUNT)

DEF_SIGNAL(COMMENT)

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    lb_content = [[UILabel alloc]init];
    btn_count = [[UIButton alloc]init];
    btn_comment = [[UIButton alloc]init];
    
    [btn_count addTarget:self action:@selector(countBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [btn_comment addTarget:self action:@selector(commentBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [btn_count setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn_count.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn_comment setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn_comment.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:lb_content];
    [self.contentView addSubview:btn_comment];
    [self.contentView addSubview:btn_count];
    return self;
}

-(void)layoutSubviews{
    lb_content.frame = CGRectMake(10, 0, 300, 44);
    btn_count.frame = CGRectMake(200, 20, 50, 14);
    btn_comment.frame = CGRectMake(260, 20, 50, 14);
}

-(void)setData:(NSDictionary *)data{
    _data = data;
    if (data) {
        lb_content.text = [data stringAtPath:@"content"];
        [btn_count setTitle:[NSString stringWithFormat:@"浏览(%@)",[data stringAtPath:@"count"]] forState:UIControlStateNormal];
        [btn_comment setTitle:[NSString stringWithFormat:@"评论(%@)",[data stringAtPath:@"comment"]] forState:UIControlStateNormal];
    }else{
        lb_content.text = nil;
        [btn_count setTitle:nil forState:UIControlStateNormal];
        [btn_comment setTitle:nil forState:UIControlStateNormal];
    }
}

-(void)countBtnClicked{
    [self sendUISignal:MyCell.COUNT withObject:self.data];
}

-(void)commentBtnClicked{
    [self sendUISignal:MyCell.COMMENT withObject:self.data];
}

@end

@interface ViewController (){
    NSArray *datas;
}

@end

@implementation ViewController

-(void)handleUISignal_MyCell:(BeeUISignal *)signal{
    if ([signal is:MyCell.COUNT]) {
        NSDictionary *dict = (NSDictionary *)signal.object;
        CC(@"%@被点击",[dict stringAtPath:@"content"]);
    }else if ([signal is:MyCell.COMMENT]){
        NSDictionary *dict = (NSDictionary *)signal.object;
        CC(@"%@被评论",[dict stringAtPath:@"content"]);
    }
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据1",@"content",@"20",@"count",@"3",@"comment", nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据2",@"content",@"30",@"count",@"4",@"comment", nil];
    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据3",@"content",@"10",@"count",@"2",@"comment", nil];
    datas = [[NSArray alloc]initWithObjects:dict1,dict2,dict3, nil];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYCELL"];
    if (!cell) {
        cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MYCELL"];
    }
    cell.data = [datas objectAtIndex:indexPath.row];
    return cell;
}

@end


    在MYCell.h中通过AS_SIGNAL定义了两个静态的属性,用来自定义UISinal的名字,MYCell.m中的DEF_SIGNAL与.h中相对应。事实上,在Bee中还有很多这样类型的预定义方法,如:AS_MESSAGE 、AS_NOTIFICATION ,通过这些预定义方法,在一定程度上范规了方法的命名,通过这些预定义的属性就能知道对应的接口的调用方式。
    在Cell中通过调用 [self sendUISignal:XXX withObject:self.data]实现了信号的传递过程。在ViewController实现规范命名的方法(见上篇),就能对信号进行响应。

     实际上Bee已经为我们提供了一个方便布局的BeeUIGirdCell,它预定义了常见的一些方法,如数据的绑定,界面的布局等等。Bee中的BeeUITableBoard、BeeUIFlowBoard都采用GirdCell来定义布局,这里我们通过category为UITableView扩展使用BeeUIGirdCell的方法。

//
//  UITableView+BeeUIGirdCell.h
//
//  Created by he songhang on 13-4-24.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

#import <Foundation/Foundation.h>
#import "Bee_UIGridCell.h"

@interface UITableViewCell (BeeUIGirdCell)
@property(nonatomic,retain) BeeUIGridCell *gridCell;
@end

@interface UITableView (BeeUIGirdCell)
-(UITableViewCell *) dequeueReusableCellWithBeeUIGirdCellClass:(Class) class;
@end
#endif


//
//  UITableView+BeeUIGirdCell.m
//  618
//
//  Created by he songhang on 13-4-24.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "UITableView+BeeUIGirdCell.h"
#import "CGRect+BeeExtension.h"
#import "Bee_Precompile.h"
#include <objc/runtime.h>
#import "Bee_Runtime.h"


@implementation UITableViewCell(BeeUIGirdCell)

@dynamic gridCell;

- (void)setFrame:(CGRect)rc
{
	[super setFrame:CGRectZeroNan(rc)];
    
	[self.gridCell setFrame:self.bounds];
    //	[_gridCell layoutSubcells];
}

- (void)setCenter:(CGPoint)pt
{
	[super setCenter:pt];
	
	[self.gridCell setFrame:self.bounds];
    //	[_gridCell layoutSubcells];
}


-(void)setGridCell:(BeeUIGridCell *)gridCell{
    if (!self.gridCell) {
        objc_setAssociatedObject( self, "UITableViewCell.gridCell", gridCell, OBJC_ASSOCIATION_RETAIN );
//        self.gridCell.autoresizesSubviews = YES;
//        self.gridCell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        
        if ( gridCell.superview != self.contentView )
        {
            [gridCell.superview removeFromSuperview];
        }
        [self.contentView addSubview:gridCell];
        
        
    }else{
        if ( self.gridCell != gridCell )
        {
            [self.gridCell release];
            objc_setAssociatedObject( self, "UITableViewCell.gridCell", gridCell, OBJC_ASSOCIATION_RETAIN );
//            self.gridCell.autoresizesSubviews = YES;
//            self.gridCell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
            
            if ( gridCell.superview != self.contentView )
            {
                [gridCell.superview removeFromSuperview];
            }
            [self.contentView addSubview:gridCell];
            
        }
    }
}

-(BeeUIGridCell *)gridCell{
    NSObject * obj = objc_getAssociatedObject( self, "UITableViewCell.gridCell" );
	if ( obj && [obj isKindOfClass:[BeeUIGridCell class]] )
		return (BeeUIGridCell *)obj;
	return nil;
}

@end


@implementation UITableView (BeeUIGirdCell)

-(UITableViewCell *) dequeueReusableCellWithBeeUIGirdCellClass:(Class) clazz{
    UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:[clazz description]];
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[cell description]]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.editingAccessoryType = UITableViewCellAccessoryNone;
		cell.showsReorderControl = NO;
		cell.shouldIndentWhileEditing = NO;
		cell.indentationLevel = 0;
		cell.indentationWidth = 0.0f;
		self.alpha = 1.0f;
		self.layer.masksToBounds = YES;
		self.layer.opaque = YES;
        
		cell.contentView.layer.masksToBounds = YES;
		cell.contentView.layer.opaque = YES;
		cell.contentView.autoresizesSubviews = YES;
        if ( [clazz isSubclassOfClass:[BeeUIGridCell class]] )
		{
			cell.gridCell = [(BeeUIGridCell *)[[BeeRuntime allocByClass:clazz] init] autorelease];
		}
    }
    return cell;
}

@end

为了下次重用这两个文件,把UITableView+BeeUIGirdCell.h和UITableView+BeeUIGirdCell.m放到Pods/BeeFramework/BeeFramework/MVC/View下,并在Pods/Headers下新建UITableView+BeeUIGirdCell.h的替身,
ln -s ../../BeeFramework/BeeFramework/MVC/View/UITableView+BeeUIGirdCell.h UITableView+BeeUIGirdCell.h


我们重新实现一下ViewController
//
//  ViewController1.h
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-4.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController1 : UITableViewController

@end


//
//  ViewController1.m
//  BeeFrameWorkTest
//
//  Created by he songhang on 13-6-4.
//  Copyright (c) 2013年 he songhang. All rights reserved.
//

#import "ViewController1.h"
#import <BeeFramework/UITableView+BeeUIGirdCell.h>
#import <BeeFramework/Bee.h>

@interface MYGirdCell : BeeUIGridCell{
    UILabel *lb_content;
    UIButton *btn_count;
    UIButton *btn_comment;
}

AS_SIGNAL(COUNT)
AS_SIGNAL(COMMENT)

@end

@implementation MYGirdCell

DEF_SIGNAL(COUNT)

DEF_SIGNAL(COMMENT)

//初始化
-(void)load{
    lb_content = [[UILabel alloc]init];
    btn_count = [[UIButton alloc]init];
    btn_comment = [[UIButton alloc]init];
    
    [btn_count addTarget:self action:@selector(countBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [btn_comment addTarget:self action:@selector(commentBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [btn_count setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn_count.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn_comment setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn_comment.titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:lb_content];
    [self addSubview:btn_comment];
    [self addSubview:btn_count];
}

//释放
-(void)unload{
    
}

//数据变化时
- (void)dataDidChanged
{
    if (self.cellData) {
        NSDictionary *data = _cellData;
        lb_content.text = [data stringAtPath:@"content"];
        [btn_count setTitle:[NSString stringWithFormat:@"浏览(%@)",[data stringAtPath:@"count"]] forState:UIControlStateNormal];
        [btn_comment setTitle:[NSString stringWithFormat:@"评论(%@)",[data stringAtPath:@"comment"]] forState:UIControlStateNormal];
    }else{
        lb_content.text = nil;
        [btn_count setTitle:nil forState:UIControlStateNormal];
        [btn_comment setTitle:nil forState:UIControlStateNormal];
    }
}

//用于计算高度,可实现动态高度
+ (CGSize)sizeInBound:(CGSize)bound forData:(NSObject *)data
{
	return bound;
}

//用于布局
- (void)layoutInBound:(CGSize)bound forCell:(BeeUIGridCell *)cell
{
    lb_content.frame = CGRectMake(10, 0, 300, 44);
    btn_count.frame = CGRectMake(200, 20, 50, 14);
    btn_comment.frame = CGRectMake(260, 20, 50, 14);
}

-(void)countBtnClicked{
    [self sendUISignal:MYGirdCell.COUNT];
}

-(void)commentBtnClicked{
    [self sendUISignal:MYGirdCell.COMMENT withObject:self.cellData];
}

@end

@interface ViewController1 (){
    NSArray *datas;
}


@end

@implementation ViewController1

-(void)handleUISignal_MYGirdCell:(BeeUISignal *)signal{
    if ([signal is:MYGirdCell.COUNT]) {
        MYGirdCell *cell = signal.source;
        NSDictionary *dict = (NSDictionary *)cell.cellData;
        CC(@"%@被点击",[dict stringAtPath:@"content"]);
    }else if ([signal is:MYGirdCell.COMMENT]){
        NSDictionary *dict = (NSDictionary *)signal.object;
        CC(@"%@被评论",[dict stringAtPath:@"content"]);
    }
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据1",@"content",@"20",@"count",@"3",@"comment", nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据2",@"content",@"30",@"count",@"4",@"comment", nil];
    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据3",@"content",@"10",@"count",@"2",@"comment", nil];
    datas = [[NSArray alloc]initWithObjects:dict1,dict2,dict3, nil];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    UITableViewCell *cell = [tableView dequeueReusableCellWithBeeUIGirdCellClass:[MYGirdCell class]];
    cell.gridCell.cellData = [datas objectAtIndex:indexPath.row];
    return cell;
}

@end


可以留意下MYGirdCell中的
-(void)countBtnClicked{
    [self sendUISignal:MYGirdCell.COUNT];
}

-(void)commentBtnClicked{
    [self sendUISignal:MYGirdCell.COMMENT withObject:self.cellData];
}


对应于ViewController1中的
-(void)handleUISignal_MYGirdCell:(BeeUISignal *)signal{
    if ([signal is:MYGirdCell.COUNT]) {
        MYGirdCell *cell = signal.source;
        NSDictionary *dict = (NSDictionary *)cell.cellData;
        CC(@"%@被点击",[dict stringAtPath:@"content"]);
    }else if ([signal is:MYGirdCell.COMMENT]){
        NSDictionary *dict = (NSDictionary *)signal.object;
        CC(@"%@被评论",[dict stringAtPath:@"content"]);
    }
}


本篇以UItableViewController演示了如何自定义UISignal,以及BeeUIGirdCell的用法。
以上代码下载地址:https://github.com/ilikeido/BeeFrameworkTest/tree/master/lesson3
  • 大小: 57.7 KB
分享到:
评论

相关推荐

    BeeFramework开源框架

    **BeeFramework开源框架** BeeFramework是一款专为iOS平台设计的高效、快速的开发框架,旨在简化iOS应用的开发过程,提高开发效率。由极客为极客打造,它集成了许多实用的功能和模块,使得开发者能够更加专注于业务...

    BeeFramework

    5. **UI组件丰富**:提供了一系列自定义的 UI 控件,如表格视图、标签栏等,使得界面设计更加灵活。 6. **事件处理**:通过事件总线机制,简化了事件传递和监听,降低了组件间的耦合度。 7. **错误处理**:强大的...

    beeframework

    beeframework的文档,是郭大大的成熟座屏

    BeeFramework_Android, 极简化的Android App开发框架和App内调试工具.zip

    **BeeFramework_Android** 是一个专为简化Android应用程序开发设计的开源框架,它强调了高效、简洁的编码方式,并内置了强大的App内调试工具,旨在帮助开发者快速构建功能丰富的移动应用。通过这个框架,开发者可以...

    iOS:在Storyboard上使用BeeFramework使用网格表视图

    在这个场景中,我们探讨的是如何在Storyboard上使用BeeFramework实现一个网格表视图。BeeFramework是一个开源的iOS开发框架,它提供了许多便利的功能,包括数据绑定、网络请求、缓存管理和视图管理等,能够帮助...

    基于MVC的IOS快速开发框架 BeeFramework.zip

    BeeFramework是一款iOS平台的MVC应用快速开发框架,使用Objective-C开发。 其早期原型曾经被应用在QQ空间 、QQ游戏大厅 等多款精品APP中。 BeeFramework 从根本上解决了iOS开发者长期困扰的各种问题,诸如:分层架构...

    电子商务移动客户端ECMobile.zip

    ECMobile 是 Geek Zoo Studio 团队推出的一款基于ECShop构建的移动商城客户 端,能够帮助企业和个人快速构建手机移动商城,并减少二次开发带来的成本。 ECMobile 是基于自主知识产权框架 BeeFramework 进行研发的跨...

    安卓BEEframwork 框架

    **安卓BEE Framework框架详解** BEE Framework是一个专为安卓...开发者在使用`BeeFramework_Android-master`这样的项目源码时,可以深入研究每个部分,了解其工作原理,从而更好地利用这个框架来构建自己的应用程序。

    Android App开发框架和App内调试工具

    这个是极简化的Android App开发框架和App内调试工具,源码BeeFramework_Android,BeeFramework Android版主要为Android初级开发人员提供一个基于MVC开发模式的APP DEMO,并提供一套APP内调试工具,包括 查看网络数据...

    2013年度中国优秀开源项目列表

    13. BeeFramework:BeeFramework是iOS平台的快速开发框架,特点包括易学易用、组件丰富,提供In-App调试和模版化技术。 14. beego:beego是一个类似tornado的Go应用框架,采用RESTful方式实现,是一个超轻量级的...

    ECMobile手机商城系统

    ECMobile手机商城系统源码PHP版,该源码系统ECMobile是基于ECShop...相关源码下载:android版源码:http:///view/4495.htmlIOS版源码:http:///view/4497.htmlBeeFramework: https://github.com/gavinkwoe/BeeFramework

    基于MVVM的IOS开发框架EasyIOS.zip

    部分函数借鉴了BeeFramework 常用类库: Action 负责网络数据请求 Model 负责数据存储 SceneModel 负责Scene与Model的绑定,调用action进行数据请求 Scene 一个视图相当于UIViewController,提供了...

    O2OMobile_Universal:O2Omobile 是 Geek Zoo Studio 推出的一款原生开源O2O系统

    PHP源代码:iOS源代码:Android源代码:BeeFramework:请FORK和STAR,感谢您的支持!官方技术支持论坛: QQ群: / /用户授权协议最终用户授权协议适用且仅适用于【O2OMobile】及后续升级版本,【Geek Zoo Studio】 团队...

    O2OMobile_IOS:O2Omobile 是 Geek Zoo Studio 推出的一款原生开源O2O系统

    PHP源代码:iOS源代码:Android源代码:BeeFramework:请FORK和STAR,感谢您的支持!官方技术支持论坛: QQ群:用户授权协议最终用户授权协议适用且仅适用于【O2OMobile】及后续升级版本,【Geek Zoo Studio】 团队拥有对...

    移动端混合开发框架分析.docx编程资料

    - 在某些特定场景下,性能可能不是最优选择。 **为什么选择类WindVane框架:** - 提供了更好的跨平台支持。 - 更容易集成现有Web技术栈。 - 性能表现良好,能够满足大多数应用场景的需求。 综上所述,移动端开发...

    支持表格控件效果

    该实现了怎样支持表格控件效果,该功能采用了BeeFramework实现的,并且可以根据数据源显示成GridView格式的列表效果,也就是说呈现表格样式效果,而且每一行都可以有多列内容,不需要我们计算每列的位置,宽度,只要...

Global site tag (gtag.js) - Google Analytics