链式开发(iOS控件的运用)

前言

  • 接触到链式开发,是在使用Mansory时,感觉写法很是牛X,于是,开始看各种链式编程的思想讲解以及案例,并对一些常用UI控件,进行了封装尝试,下面逐个列出代码,以便以后更好的搬运。

链式编程的利弊

优点

  1. 代码简洁;
  2. 开发速度可以加快
  3. 增强可读性

缺点

需要比较强大的代码能力(hahahahaha自恋一波)

运用实例

TableView

创建UITableView的category,并在新建文件中加入以下代码
  • .h文件
 @interface UITableView (ChainStyle)

+ (UITableView *)makeTableViewPlain:(void (^)(UITableView *))block;//创建

+ (UITableView *)makeTableViewGroup:(void (^)(UITableView *))block;

- (UITableView *(^)(CGRect rect))mFrame;//设置frame

- (UITableView *(^)(CGFloat rowHeight))mRowHeight;//行高

- (UITableView *(^)(UIColor * bgColor))mBgColor;//背景色

- (UITableView *(^)(BOOL showVerticalScrollIndicator))mShowVertical;//是否展示纵向滑动条

- (UITableView *(^)(BOOL showHorizontalScrollIndicator))mShowHorizontal;//是否展示横向滑动条

- (UITableView *(^)(UITableViewCellSeparatorStyle separatorStyle))mSeparatorStyle;//cell分隔方式

- (UITableView *(^)(Class className, NSString *identifier))mTableViewCellClass;//cell的class及标识

- (UITableView *(^)(id delegate, id dataSource))mDelegate;//设置代理
@end
  • .m文件
@implementation UITableView (ChainStyle)

+ (UITableView *)makeTableViewPlain:(void (^)(UITableView *))block {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    block(tableView);
    return tableView;
}
+ (UITableView *)makeTableViewGroup:(void (^)(UITableView *))block {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    block(tableView);
    return tableView;
}
- (UITableView *(^)(CGRect))mFrame {
    return ^id(CGRect rect) {
        self.frame = rect;
        return self;
    };
}
- (UITableView *(^)(CGFloat))mRowHeight {
    return ^id(CGFloat rowHeight) {
        self.rowHeight = rowHeight;
        return self;
    };
}

- (UITableView *(^)(UIColor *))mBgColor {
    return ^id(UIColor * bgColor) {
        self.backgroundColor = bgColor;
        return self;
    };
}

- (UITableView *(^)(BOOL))mShowVertical {
    return ^id(BOOL showVerticalScrollIndicator) {
        self.showsVerticalScrollIndicator = showVerticalScrollIndicator;
        return self;
    };
}

- (UITableView *(^)(BOOL))mShowHorizontal {
    return ^id(BOOL showHorizontalScrollIndicator) {
        self.showsHorizontalScrollIndicator = showHorizontalScrollIndicator;
        return self;
    };
}

- (UITableView *(^)(UITableViewCellSeparatorStyle))mSeparatorStyle {
    return ^id(UITableViewCellSeparatorStyle separatorStyle) {
        self.separatorStyle = separatorStyle;
        return self;
    };
}

-(UITableView *(^)(__unsafe_unretained Class, NSString *))mTableViewCellClass {
    return ^id(Class className, NSString *identifier) {
        [self registerClass:className forCellReuseIdentifier:identifier];
        return self;
    };
}
- (UITableView *(^)(id, id))mDelegate {
    return ^id(id delegate ,id dataSource) {
        self.delegate = delegate;
        self.dataSource = dataSource;
        return self;
    };
}
@end
应用
  • 在需要使用的文件中,引用刚才建的类目名称
#import "UITableView+ChainStyle.h"
  • 然后创建
- (void)makeTableView {
    if (_tableView == nil) {
        [UITableView makeTableViewPlain:^(UITableView *tableView) {
            _tableView = tableView;
            tableView.mFrame(CGRectMake(0, 0, Screen_width, Screen_height - 64)).mRowHeight(40).mShowVertical(YES).mShowHorizontal(NO).mBgColor([UIColor whiteColor]).mSeparatorStyle(UITableViewCellSeparatorStyleNone).mTableViewCellClass([UITableViewCell class], @"Animation").mDelegate(self, self).addView(self.view);
        }];
    }
}
  • viewDidLoad中
[self makeTableView];
代码如上,还可对button,label等空间进行同样的封装,在以后的项目中使用(在tableView的style设置上,因为UITableViewStyle是readonly,所以没有实现style的链式开发,希望有大神可以提出修改方法)

结束

你可能感兴趣的:(链式开发(iOS控件的运用))