zoukankan      html  css  js  c++  java
  • BeeFramework 系列二 UISignal篇下

    本文转载至 http://www.apkbus.com/android-126129-1-1.html

       

    该用户从未签到

    156

    主题

    156

    帖子

    1826

    积分

    Android子爵

    积分
    1826
    跳转到指定楼层
    楼主
     
     发表于 2013-7-16 14:26:31 只看该作者 回帖奖励
    上篇,我们讲了UISignal的工作原理,以及BeeUIButton中的一些用法,实际上,BeeFramework框架为大部分常用组件封装了UISignal,在应用中只需要对Signal进行处理就好了,这在一定程度上减轻了代码量。 

          在实际应用中UITableView的场景可谓是无处不在,下面的例子实现了一个UITableViewCell的自定义UISignal。先看下效果图 
           <ignore_js_op> 
          点击浏览或评论触发相应事件,为了响应这样的事件,通常的做法是在UITableViewCell中采用代理的方式,在ViewController中实现Cell的协议。 
          下面看下Bee的写法
    1. //
    2. //  ViewController.h
    3. //  BeeFrameWorkTest
    4. //
    5. //  Created by he songhang on 13-6-3.
    6. //  Copyright (c) 2013年 he songhang. All rights reserved.
    7. //
    8. #import <UIKit/UIKit.h>
    9. #import <BeeFramework/Bee.h>
    10. @interface MyCell : UITableViewCell{
    11.     UILabel *lb_content;
    12.     UIButton *btn_count;
    13.     UIButton *btn_comment;
    14. }
    15. @property(nonatomic,retain) NSDictionary *data;
    16. AS_SIGNAL(COUNT)
    17. AS_SIGNAL(COMMENT)
    18. @end
    19. @interface ViewController : UITableViewController
    20. @end
    复制代码
    点击浏览或评论触发相应事件,为了响应这样的事件,通常的做法是在UITableViewCell中采用代理的方式,在ViewController中实现Cell的协议。 
          下面看下Bee的写法
    1. //
    2. //  ViewController.h
    3. //  BeeFrameWorkTest
    4. //
    5. //  Created by he songhang on 13-6-3.
    6. //  Copyright (c) 2013年 he songhang. All rights reserved.
    7. //
    8. #import <UIKit/UIKit.h>
    9. #import <BeeFramework/Bee.h>
    10. @interface MyCell : UITableViewCell{
    11.     UILabel *lb_content;
    12.     UIButton *btn_count;
    13.     UIButton *btn_comment;
    14. }
    15. @property(nonatomic,retain) NSDictionary *data;
    16. AS_SIGNAL(COUNT)
    17. AS_SIGNAL(COMMENT)
    18. @end
    19. @interface ViewController : UITableViewController
    20. @end
    复制代码
    1. //
    2. //  ViewController.m
    3. //  BeeFrameWorkTest
    4. //
    5. //  Created by he songhang on 13-6-3.
    6. //  Copyright (c) 2013年 he songhang. All rights reserved.
    7. //
    8. #import "ViewController.h"
    9. @implementation MyCell
    10. DEF_SIGNAL(COUNT)
    11. DEF_SIGNAL(COMMENT)
    12. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    13.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    14.     lb_content = [[UILabel alloc]init];
    15.     btn_count = [[UIButton alloc]init];
    16.     btn_comment = [[UIButton alloc]init];
    17.     
    18.     [btn_count addTarget:self action:@selector(countBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    19.     [btn_comment addTarget:self action:@selector(commentBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    20.     [btn_count setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    21.     btn_count.titleLabel.font = [UIFont systemFontOfSize:12];
    22.     [btn_comment setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    23.     btn_comment.titleLabel.font = [UIFont systemFontOfSize:12];
    24.     [self.contentView addSubview:lb_content];
    25.     [self.contentView addSubview:btn_comment];
    26.     [self.contentView addSubview:btn_count];
    27.     return self;
    28. }
    29. -(void)layoutSubviews{
    30.     lb_content.frame = CGRectMake(10, 0, 300, 44);
    31.     btn_count.frame = CGRectMake(200, 20, 50, 14);
    32.     btn_comment.frame = CGRectMake(260, 20, 50, 14);
    33. }
    34. -(void)setData:(NSDictionary *)data{
    35.     _data = data;
    36.     if (data) {
    37.         lb_content.text = [data stringAtPath:@"content"];
    38.         [btn_count setTitle:[NSString stringWithFormat:@"浏览(%@)",[data stringAtPath:@"count"]] forState:UIControlStateNormal];
    39.         [btn_comment setTitle:[NSString stringWithFormat:@"评论(%@)",[data stringAtPath:@"comment"]] forState:UIControlStateNormal];
    40.     }else{
    41.         lb_content.text = nil;
    42.         [btn_count setTitle:nil forState:UIControlStateNormal];
    43.         [btn_comment setTitle:nil forState:UIControlStateNormal];
    44.     }
    45. }
    46. -(void)countBtnClicked{
    47.     [self sendUISignal:MyCell.COUNT withObject:self.data];
    48. }
    49. -(void)commentBtnClicked{
    50.     [self sendUISignal:MyCell.COMMENT withObject:self.data];
    51. }
    52. @end
    53. @interface ViewController (){
    54.     NSArray *datas;
    55. }
    56. @end
    57. @implementation ViewController
    58. -(void)handleUISignal_MyCell:(BeeUISignal *)signal{
    59.     if ([signal is:MyCell.COUNT]) {
    60.         NSDictionary *dict = (NSDictionary *)signal.object;
    61.         CC(@"%@被点击",[dict stringAtPath:@"content"]);
    62.     }else if ([signal is:MyCell.COMMENT]){
    63.         NSDictionary *dict = (NSDictionary *)signal.object;
    64.         CC(@"%@被评论",[dict stringAtPath:@"content"]);
    65.     }
    66. }
    67. - (id)initWithStyle:(UITableViewStyle)style
    68. {
    69.     self = [super initWithStyle:style];
    70.     if (self) {
    71.         // Custom initialization
    72.     }
    73.     return self;
    74. }
    75. - (void)viewDidLoad
    76. {
    77.     [super viewDidLoad];
    78.     NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据1",@"content",@"20",@"count",@"3",@"comment", nil];
    79.     NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据2",@"content",@"30",@"count",@"4",@"comment", nil];
    80.     NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据3",@"content",@"10",@"count",@"2",@"comment", nil];
    81.     datas = [[NSArray alloc]initWithObjects:dict1,dict2,dict3, nil];
    82.         // Do any additional setup after loading the view, typically from a nib.
    83. }
    84. - (void)didReceiveMemoryWarning
    85. {
    86.     [super didReceiveMemoryWarning];
    87.     // Dispose of any resources that can be recreated.
    88. }
    89. #pragma mark -UITableViewDataSource
    90. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    91.     return [datas count];
    92. }
    93. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    94.     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYCELL"];
    95.     if (!cell) {
    96.         cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MYCELL"];
    97.     }
    98.     cell.data = [datas objectAtIndex:indexPath.row];
    99.     return cell;
    100. }
    101. @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的方法。
    1. //
    2. //  UITableView+BeeUIGirdCell.h
    3. //
    4. //  Created by he songhang on 13-4-24.
    5. //  Copyright (c) 2013年 he songhang. All rights reserved.
    6. //
    7. #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    8. #import <Foundation/Foundation.h>
    9. #import "Bee_UIGridCell.h"
    10. @interface UITableViewCell (BeeUIGirdCell)
    11. @property(nonatomic,retain) BeeUIGridCell *gridCell;
    12. @end
    13. @interface UITableView (BeeUIGirdCell)
    14. -(UITableViewCell *) dequeueReusableCellWithBeeUIGirdCellClass:(Class) class;
    15. @end
    16. #endif
    复制代码
    1. //
    2. //  UITableView+BeeUIGirdCell.m
    3. //  618
    4. //
    5. //  Created by he songhang on 13-4-24.
    6. //  Copyright (c) 2013年 he songhang. All rights reserved.
    7. //
    8. #import "UITableView+BeeUIGirdCell.h"
    9. #import "CGRect+BeeExtension.h"
    10. #import "Bee_Precompile.h"
    11. #include <objc/runtime.h>
    12. #import "Bee_Runtime.h"
    13. @implementation UITableViewCell(BeeUIGirdCell)
    14. @dynamic gridCell;
    15. - (void)setFrame:(CGRect)rc
    16. {
    17.         [super setFrame:CGRectZeroNan(rc)];
    18.     
    19.         [self.gridCell setFrame:self.bounds];
    20.     //        [_gridCell layoutSubcells];
    21. }
    22. - (void)setCenter:(CGPoint)pt
    23. {
    24.         [super setCenter:pt];
    25.         
    26.         [self.gridCell setFrame:self.bounds];
    27.     //        [_gridCell layoutSubcells];
    28. }
    29. -(void)setGridCell:(BeeUIGridCell *)gridCell{
    30.     if (!self.gridCell) {
    31.         objc_setAssociatedObject( self, "UITableViewCell.gridCell", gridCell, OBJC_ASSOCIATION_RETAIN );
    32. //        self.gridCell.autoresizesSubviews = YES;
    33. //        self.gridCell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    34.         
    35.         if ( gridCell.superview != self.contentView )
    36.         {
    37.             [gridCell.superview removeFromSuperview];
    38.         }
    39.         [self.contentView addSubview:gridCell];
    40.         
    41.         
    42.     }else{
    43.         if ( self.gridCell != gridCell )
    44.         {
    45.             [self.gridCell release];
    46.             objc_setAssociatedObject( self, "UITableViewCell.gridCell", gridCell, OBJC_ASSOCIATION_RETAIN );
    47. //            self.gridCell.autoresizesSubviews = YES;
    48. //            self.gridCell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    49.             
    50.             if ( gridCell.superview != self.contentView )
    51.             {
    52.                 [gridCell.superview removeFromSuperview];
    53.             }
    54.             [self.contentView addSubview:gridCell];
    55.             
    56.         }
    57.     }
    58. }
    59. -(BeeUIGridCell *)gridCell{
    60.     NSObject * obj = objc_getAssociatedObject( self, "UITableViewCell.gridCell" );
    61.         if ( obj && [obj isKindOfClass:[BeeUIGridCell class]] )
    62.                 return (BeeUIGridCell *)obj;
    63.         return nil;
    64. }
    65. @end
    66. @implementation UITableView (BeeUIGirdCell)
    67. -(UITableViewCell *) dequeueReusableCellWithBeeUIGirdCellClass:(Class) clazz{
    68.     UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:[clazz description]];
    69.     if (!cell) {
    70.         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[cell description]]autorelease];
    71.         cell.selectionStyle = UITableViewCellSelectionStyleNone;
    72.                 cell.accessoryType = UITableViewCellAccessoryNone;
    73.                 cell.editingAccessoryType = UITableViewCellAccessoryNone;
    74.                 cell.showsReorderControl = NO;
    75.                 cell.shouldIndentWhileEditing = NO;
    76.                 cell.indentationLevel = 0;
    77.                 cell.indentationWidth = 0.0f;
    78.                 self.alpha = 1.0f;
    79.                 self.layer.masksToBounds = YES;
    80.                 self.layer.opaque = YES;
    81.         
    82.                 cell.contentView.layer.masksToBounds = YES;
    83.                 cell.contentView.layer.opaque = YES;
    84.                 cell.contentView.autoresizesSubviews = YES;
    85.         if ( [clazz isSubclassOfClass:[BeeUIGridCell class]] )
    86.                 {
    87.                         cell.gridCell = [(BeeUIGridCell *)[[BeeRuntime allocByClass:clazz] init] autorelease];
    88.                 }
    89.     }
    90.     return cell;
    91. }
    92. @end
    复制代码
    为了下次重用这两个文件,把UITableView+BeeUIGirdCell.h和UITableView+BeeUIGirdCell.m放到Pods/BeeFramework/BeeFramework/MVC/View下,并在Pods/Headers下新建UITableView+BeeUIGirdCell.h的替身,
    1. ln -s ../../BeeFramework/BeeFramework/MVC/View/UITableView+BeeUIGirdCell.h UITableView+BeeUIGirdCell.h
    复制代码
    我们重新实现一下ViewController
    1. //
    2. //  ViewController1.h
    3. //  BeeFrameWorkTest
    4. //
    5. //  Created by he songhang on 13-6-4.
    6. //  Copyright (c) 2013年 he songhang. All rights reserved.
    7. //
    8. #import <UIKit/UIKit.h>
    9. @interface ViewController1 : UITableViewController
    10. @end
    复制代码
    1. //
    2. //  ViewController1.m
    3. //  BeeFrameWorkTest
    4. //
    5. //  Created by he songhang on 13-6-4.
    6. //  Copyright (c) 2013年 he songhang. All rights reserved.
    7. //
    8. #import "ViewController1.h"
    9. #import <BeeFramework/UITableView+BeeUIGirdCell.h>
    10. #import <BeeFramework/Bee.h>
    11. @interface MYGirdCell : BeeUIGridCell{
    12.     UILabel *lb_content;
    13.     UIButton *btn_count;
    14.     UIButton *btn_comment;
    15. }
    16. AS_SIGNAL(COUNT)
    17. AS_SIGNAL(COMMENT)
    18. @end
    19. @implementation MYGirdCell
    20. DEF_SIGNAL(COUNT)
    21. DEF_SIGNAL(COMMENT)
    22. //初始化
    23. -(void)load{
    24.     lb_content = [[UILabel alloc]init];
    25.     btn_count = [[UIButton alloc]init];
    26.     btn_comment = [[UIButton alloc]init];
    27.     
    28.     [btn_count addTarget:self action:@selector(countBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    29.     [btn_comment addTarget:self action:@selector(commentBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    30.     [btn_count setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    31.     btn_count.titleLabel.font = [UIFont systemFontOfSize:12];
    32.     [btn_comment setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    33.     btn_comment.titleLabel.font = [UIFont systemFontOfSize:12];
    34.     [self addSubview:lb_content];
    35.     [self addSubview:btn_comment];
    36.     [self addSubview:btn_count];
    37. }
    38. //释放
    39. -(void)unload{
    40.     
    41. }
    42. //数据变化时
    43. - (void)dataDidChanged
    44. {
    45.     if (self.cellData) {
    46.         NSDictionary *data = _cellData;
    47.         lb_content.text = [data stringAtPath:@"content"];
    48.         [btn_count setTitle:[NSString stringWithFormat:@"浏览(%@)",[data stringAtPath:@"count"]] forState:UIControlStateNormal];
    49.         [btn_comment setTitle:[NSString stringWithFormat:@"评论(%@)",[data stringAtPath:@"comment"]] forState:UIControlStateNormal];
    50.     }else{
    51.         lb_content.text = nil;
    52.         [btn_count setTitle:nil forState:UIControlStateNormal];
    53.         [btn_comment setTitle:nil forState:UIControlStateNormal];
    54.     }
    55. }
    56. //用于计算高度,可实现动态高度
    57. + (CGSize)sizeInBound:(CGSize)bound forData:(NSObject *)data
    58. {
    59.         return bound;
    60. }
    61. //用于布局
    62. - (void)layoutInBound:(CGSize)bound forCell:(BeeUIGridCell *)cell
    63. {
    64.     lb_content.frame = CGRectMake(10, 0, 300, 44);
    65.     btn_count.frame = CGRectMake(200, 20, 50, 14);
    66.     btn_comment.frame = CGRectMake(260, 20, 50, 14);
    67. }
    68. -(void)countBtnClicked{
    69.     [self sendUISignal:MYGirdCell.COUNT];
    70. }
    71. -(void)commentBtnClicked{
    72.     [self sendUISignal:MYGirdCell.COMMENT withObject:self.cellData];
    73. }
    74. @end
    75. @interface ViewController1 (){
    76.     NSArray *datas;
    77. }
    78. @end
    79. @implementation ViewController1
    80. -(void)handleUISignal_MYGirdCell:(BeeUISignal *)signal{
    81.     if ([signal is:MYGirdCell.COUNT]) {
    82.         MYGirdCell *cell = signal.source;
    83.         NSDictionary *dict = (NSDictionary *)cell.cellData;
    84.         CC(@"%@被点击",[dict stringAtPath:@"content"]);
    85.     }else if ([signal is:MYGirdCell.COMMENT]){
    86.         NSDictionary *dict = (NSDictionary *)signal.object;
    87.         CC(@"%@被评论",[dict stringAtPath:@"content"]);
    88.     }
    89. }
    90. - (id)initWithStyle:(UITableViewStyle)style
    91. {
    92.     self = [super initWithStyle:style];
    93.     if (self) {
    94.         // Custom initialization
    95.     }
    96.     return self;
    97. }
    98. - (void)viewDidLoad
    99. {
    100.     [super viewDidLoad];
    101.     NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据1",@"content",@"20",@"count",@"3",@"comment", nil];
    102.     NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据2",@"content",@"30",@"count",@"4",@"comment", nil];
    103.     NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"测试数据3",@"content",@"10",@"count",@"2",@"comment", nil];
    104.     datas = [[NSArray alloc]initWithObjects:dict1,dict2,dict3, nil];
    105.         // Do any additional setup after loading the view, typically from a nib.
    106. }
    107. - (void)didReceiveMemoryWarning
    108. {
    109.     [super didReceiveMemoryWarning];
    110.     // Dispose of any resources that can be recreated.
    111. }
    112. #pragma mark -UITableViewDataSource
    113. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    114.     return [datas count];
    115. }
    116. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    117.     UITableViewCell *cell = [tableView dequeueReusableCellWithBeeUIGirdCellClass:[MYGirdCell class]];
    118.     cell.gridCell.cellData = [datas objectAtIndex:indexPath.row];
    119.     return cell;
    120. }
    121. @end
    复制代码
    可以留意下MYGirdCell中的
    1. -(void)countBtnClicked{
    2.     [self sendUISignal:MYGirdCell.COUNT];
    3. }
    4. -(void)commentBtnClicked{
    5.     [self sendUISignal:MYGirdCell.COMMENT withObject:self.cellData];
    6. }
    复制代码
    对应于ViewController1中的
    1. -(void)handleUISignal_MYGirdCell:(BeeUISignal *)signal{
    2.     if ([signal is:MYGirdCell.COUNT]) {
    3.         MYGirdCell *cell = signal.source;
    4.         NSDictionary *dict = (NSDictionary *)cell.cellData;
    5.         CC(@"%@被点击",[dict stringAtPath:@"content"]);
    6.     }else if ([signal is:MYGirdCell.COMMENT]){
    7.         NSDictionary *dict = (NSDictionary *)signal.object;
    8.         CC(@"%@被评论",[dict stringAtPath:@"content"]);
    9.     }
    10. }
    复制代码
    本篇以UItableViewController演示了如何自定义UISignal,以及BeeUIGirdCell的用法。 
    以上代码下载地址:https://github.com/ilikeido/BeeFrameworkTest/tree/master/lesson3
  • 相关阅读:
    重排列
    最多分成多少块
    后面第一个大于
    走格子
    硬币游戏
    还是01串
    戴德兰
    个人所得税
    最长高地
    执行Commit时Oracle做哪些工作
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3821780.html
Copyright © 2011-2022 走看看