自造輪子:YQCommonCell 簡化表單視圖開發(fā)

前言

由于寫了太多次的設(shè)置界面、個人中心界面等,已經(jīng)寫到麻木。漸漸意識到,或許可以把這些類似的TableViewCell封裝成一種,通過簡單的方法去創(chuàng)建以及使用,這樣就可以大大減少勞動力啦。

在這篇文章之前,我已經(jīng)寫了一篇介紹YQCommonCell的文章,但是因?yàn)楦碌拇螖?shù)過多內(nèi)容雜亂,我決定重新寫一篇。

項(xiàng)目中的使用效果

很榮幸,在公司的項(xiàng)目中,已經(jīng)使用了這個框架。的確,在開發(fā)表單視圖上節(jié)約了大量時間。下面是兩個項(xiàng)目中的效果圖:

項(xiàng)目效果1.PNG

項(xiàng)目效果2.PNG

內(nèi)部結(jié)構(gòu)

內(nèi)部結(jié)構(gòu).png

其實(shí)這個項(xiàng)目的思路很容易理解,我將UITableView的數(shù)據(jù)源抽離成模型,cell對應(yīng)YQCommonItem,section對應(yīng)YQCommonGroup。在YQCommonCell類有@property (nonatomic, strong) YQCommonItem *item;屬性。在-(void)setItem:(YQCommonItem *)item方法中進(jìn)行屬性設(shè)置,在- (void)layoutSubviews中布局界面。

能做的

這個框架幾乎能解決所有的表單視圖繪制。

YQCommonItem的屬性

cell自身相關(guān)屬性介紹

// cell 對應(yīng)屬性
YQPropStatementAndFuncStatement(assign,  YQCommonItem, BOOL,                         selectAbility) ///< cell能否被點(diǎn)擊 default YES.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, BOOL,                         selectHighlight) ///< cell被點(diǎn)擊是否高亮 default YES.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, BOOL,                         arrow) ///< 是否有箭頭 default NO.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      cellHeight) ///< cell的高度 default 44.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, Class,                        destVcClass) ///< 點(diǎn)擊后的跳轉(zhuǎn)控制器 default nil.
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIColor *,                    cellBackgroudColor) ///< cell背景顏色 default [UIColor whiteColor].
YQPropStatementAndFuncStatement(copy,    YQCommonItem, YQCommonCellSelectBlock,      operation) ///< 點(diǎn)擊后的跳轉(zhuǎn)控制器 default nil.

icon 和提醒紅點(diǎn)相關(guān)屬性介紹

// icon 對應(yīng)屬性
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   icon) ///< 圖標(biāo) default nil.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      iconWidth) ///< 圖標(biāo)大小 default.

// badge 對應(yīng)屬性
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   badgeValue) ///< 紅點(diǎn)提醒 default nil.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, YQBadgeViewLayout,            badgeLayout) ///< 紅點(diǎn)提醒布局 default YQBadgeViewLayoutLeft.

效果:

icon&badge.png

title相關(guān)屬性介紹

// title 對應(yīng)屬性
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   title) ///< 標(biāo)題 default nil.
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIFont *,                     titleLableFont) ///< 標(biāo)題font default [UIFont systemFontOfSize:16].
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIColor *,                    titleLableColor) ///< 標(biāo)題color default [UIFont BlackColor].
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSAttributedString *,         attributedTitle) ///< 標(biāo)題 default nil.

效果:

title.png

assistLabel相關(guān)屬性介紹

// 輔助視圖assistLabel 對應(yīng)屬性   NSAttributedString > NSString
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   assistLabelText) ///< 標(biāo)題 default nil. lable.textAlignment = NSTextAlignmentRight;
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIFont *,                     assistLabelFont) ///< 標(biāo)題font default [UIFont systemFontOfSize:15].
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIColor *,                    assistLabelColor) ///< 標(biāo)題color default [UIFont BlackColor].
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSAttributedString *,         assistLabelAttributedText) ///< 標(biāo)題 default nil.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      assistLabelX) ///< assistLabel X default 0. if>0 lable.textAlignment = NSTextAlignmentLeft;

效果:

assistLabel.png

assistField相關(guān)屬性展示

// 輔助視圖textField 對應(yīng)屬性
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   assistFieldText) ///< textField.text default nil. Field.textAlignment = NSTextAlignmentRight;
YQPropStatementAndFuncStatement(assign,  YQCommonItem, UIKeyboardType,               assistFieldKeyboardType) ///< textField.keyboardType
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIFont *,                     assistFieldFont) ///< textField.font [UIFont systemFontOfSize:15].
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIColor *,                    assistFieldColor) ///< textField.textColor default [UIFont BlackColor].
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   assistFieldPlaceholderText) ///< textField.placeholder default nil.
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIColor *,                    assistFieldPlaceholderColor) ///< textField.placeholderTextColor
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      assistFieldX) ///< assistField X default 0. if>0 Field.textAlignment = NSTextAlignmentLeft;
YQPropStatementAndFuncStatement(copy,    YQCommonItem, YQCommonFieldTextChangeBlock, fieldTextChangeBlock)
YQPropStatementAndFuncStatement(copy,    YQCommonItem, YQCommonFieldEditFinishBlock, fieldEditFinishBlock)

效果:

assistField.png

assistView相關(guān)屬性展示

// 輔助視圖自定義customview 對應(yīng)屬性
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIView *,                     assistCustomView) ///< CustomView default nil.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, YQAssistCustomViewLayout,     assistCustomViewLayout) ///< CustomView Layout default YQAssistCustomViewLayoutRight
YQPropStatementAndFuncStatement(assign,  YQCommonItem, BOOL,                         assistCustomViewClipsToBounds) ///< CustomView ClipsToBounds default YES.

assistView相關(guān)的屬性較少,但是這個屬性卻解決了絕大多數(shù)的復(fù)雜界面的繪制。

效果:


assistView.png

其他屬性展示

// 分割線 對應(yīng)屬性
YQPropStatementAndFuncStatement(assign,  YQCommonItem, BOOL,                         hadBottomLine) ///< BottomLine default YES.
YQPropStatementAndFuncStatement(strong,  YQCommonItem, UIColor *,                    bottomLineColor) ///< BottomLineColor default [UIColor colorWithWhite:0.85 alpha:0.6]
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      bottomLineHeight) ///< BottomLine default 1.0f.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      bottomLineX) ///< BottomLine default line.x = titlelabel.x

YQCommonGroup的屬性

YQPropStatementAndFuncStatement(strong,  YQCommonGroup, NSArray *,           items)
YQPropStatementAndFuncStatement(assign,  YQCommonGroup, BOOL,                hiddenLastRowBottomLine) ///< hidden last row bottomLine default YES.

YQPropStatementAndFuncStatement(assign,  YQCommonGroup, CGFloat,             headerHeight) ///< default 20.
YQPropStatementAndFuncStatement(copy,    YQCommonGroup, NSString *,          headerTitle) ///< default nil.
YQPropStatementAndFuncStatement(strong,  YQCommonGroup, UIColor *,           headerTitleColor) ///< default [UIColor grayColor].
YQPropStatementAndFuncStatement(strong,  YQCommonGroup, UIFont *,           headerTitleFont) ///< default [UIFont systemFontOfSize:16].
YQPropStatementAndFuncStatement(assign,  YQCommonGroup, CGFloat,             headerTitleHeight) ///< default 50.
YQPropStatementAndFuncStatement(assign,  YQCommonGroup, YQHeaderTitleLayout, headerTitleLayout) ///< default YQHeaderTitleLayoutLeft.
YQPropStatementAndFuncStatement(strong,  YQCommonGroup, UIView *,            headerView) ///< default nil.


YQPropStatementAndFuncStatement(assign,  YQCommonGroup, CGFloat,             footerHeight) ///< default 0.01.
YQPropStatementAndFuncStatement(copy,    YQCommonGroup, NSString *,          footerTitle) ///< default nil.
YQPropStatementAndFuncStatement(strong,  YQCommonGroup, UIColor *,           footerTitleColor) ///< default [UIColor grayColor].
YQPropStatementAndFuncStatement(strong,  YQCommonGroup, UIFont *,           footerTitleFont) ///< default [UIFont systemFontOfSize:16].
YQPropStatementAndFuncStatement(assign,  YQCommonGroup, CGFloat,             footerTitleHeight) ///< default 50.
YQPropStatementAndFuncStatement(assign,  YQCommonGroup, YQFooterTitleLayout, footerTitleLayout) ///< default YQFooterTitleLayoutLeft.
YQPropStatementAndFuncStatement(strong,  YQCommonGroup, UIView *,            footerView) ///< default nil.

正如上文所說,YQCommonGroup對應(yīng)的是tableviewSection,因此它的屬性都與Section相關(guān)。

其中需要注意的是hiddenLastRowBottomLine屬性,默認(rèn)開啟,會隱藏每個Section最后一行cell的分割線。

效果:

其他效果.gif

如何使用

@interface MainViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray * groups;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Main";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self setData];
    
    [self setView];
}

- (void)setData
{
    __weak typeof(self) weakSelf = self;
    
    YQCommonItem *item0 = YQObjectInit(YQCommonItem).titleSet(@"title屬性展示").arrowSet(YES).operationSet(^{
        TitleViewController *title = [[TitleViewController alloc] init];
        title.title = @"title屬性展示";
        [weakSelf.navigationController pushViewController:title animated:YES];
    });
    
    YQCommonItem *item1 = YQObjectInit(YQCommonItem).titleSet(@"icon以及badge屬性展示").arrowSet(YES).operationSet(^{
        IconAndBadgeViewController *icon = [[IconAndBadgeViewController alloc] init];
        icon.title = @"icon以及badge屬性展示";
        [weakSelf.navigationController pushViewController:icon animated:YES];
    });

    YQCommonItem *item2 = YQObjectInit(YQCommonItem).titleSet(@"assistLabel屬性展示").arrowSet(YES).operationSet(^{
        AssistLabelViewController *AssistLabel = [[AssistLabelViewController alloc] init];
        AssistLabel.title = @"assistLabel屬性展示";
        [weakSelf.navigationController pushViewController:AssistLabel animated:YES];
    });

    YQCommonItem *item3 = YQObjectInit(YQCommonItem).titleSet(@"assistFeild屬性展示").arrowSet(YES).operationSet(^{
        AssistFeildViewController *AssistFeild = [[AssistFeildViewController alloc] init];
        AssistFeild.title = @"assistFeild屬性展示";
        [weakSelf.navigationController pushViewController:AssistFeild animated:YES];
    });
    
    YQCommonItem *item4 = YQObjectInit(YQCommonItem).titleSet(@"assistView屬性展示").arrowSet(YES).operationSet(^{
        AssistViewViewController *assistView = [[AssistViewViewController alloc] init];
        assistView.title = @"assistView屬性展示";
        [weakSelf.navigationController pushViewController:assistView animated:YES];
    });

    YQCommonItem *item5 = YQObjectInit(YQCommonItem).titleSet(@"其他").arrowSet(YES).operationSet(^{
        OtherViewController *other = [[OtherViewController alloc] init];
        other.title = @"其他";
        [weakSelf.navigationController pushViewController:other animated:YES];
    });
    
    
    YQCommonGroup *group = YQObjectInit(YQCommonGroup).itemsSet(@[item0, item1, item2, item3, item4, item5]);
    
    [self.groups addObject:group];
}

- (void)setView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    self.tableView.frame = CGRectMake(0, YQTableViewY, self.view.bounds.size.width, self.view.bounds.size.height - YQTableViewY - YQIndicatorHeight);
    YQNewAdjustsScrollViewInsets(self.tableView);
}

#pragma mark - lazy
- (NSMutableArray *)groups
{
    if (!_groups) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

#pragma mark - tableview Data
YQTabelViewDelegateAndDataSource(self.groups)

以demo中的MainViewContoller為例,tableview的數(shù)據(jù)源與代理方法我都用宏寫入YQCommonMacro中。設(shè)置cell的屬性全在- (void)setData方法中實(shí)現(xiàn)。

效果:

Main.png

至于其他效果的實(shí)現(xiàn),可下載demo了解。

博客地址

jabberyq 自造輪子:YQCommonCell 簡化表單視圖開發(fā)

下載

GitHub:YQCommonCell

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,282評論 3 38
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,966評論 1 9
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,026評論 4 61
  • 我們在上一篇《通過代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識,本文將在上文的基礎(chǔ)上,利用sto...
    啊世ka閱讀 1,637評論 2 7
  • 提起馬陵山,大家就會想到孫臏和龐涓的馬陵之戰(zhàn),今兒個咱不聊這個,聊聊一些神神叨叨的事情。 01 黑龍...
    木薇清閱讀 2,231評論 6 8

友情鏈接更多精彩內(nèi)容