自造輪子:YQCommonCell 簡(jiǎn)化表單視圖開(kāi)發(fā)

前言

由于寫(xiě)了太多次的設(shè)置界面慨飘、個(gè)人中心界面等瓶殃,已經(jīng)寫(xiě)到麻木榨乎。漸漸意識(shí)到蛇捌,或許可以把這些類(lèi)似的TableViewCell封裝成一種丈甸,通過(guò)簡(jiǎn)單的方法去創(chuàng)建以及使用翰绊,這樣就可以大大減少勞動(dòng)力啦玉罐。

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

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

很榮幸啄栓,在公司的項(xiàng)目中娄帖,已經(jīng)使用了這個(gè)框架。的確昙楚,在開(kāi)發(fā)表單視圖上節(jié)約了大量時(shí)間近速。下面是兩個(gè)項(xiàng)目中的效果圖:

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

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

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

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

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

能做的

這個(gè)框架幾乎能解決所有的表單視圖繪制谭跨。

YQCommonItem的屬性

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

// cell 對(duì)應(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 對(duì)應(yīng)屬性
YQPropStatementAndFuncStatement(copy,    YQCommonItem, NSString *,                   icon) ///< 圖標(biāo) default nil.
YQPropStatementAndFuncStatement(assign,  YQCommonItem, CGFloat,                      iconWidth) ///< 圖標(biāo)大小 default.

// badge 對(duì)應(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 對(duì)應(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 對(duì)應(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 對(duì)應(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 對(duì)應(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)的屬性較少干厚,但是這個(gè)屬性卻解決了絕大多數(shù)的復(fù)雜界面的繪制。

效果:


assistView.png

其他屬性展示

// 分割線 對(duì)應(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.

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

其中需要注意的是hiddenLastRowBottomLine屬性谆扎,默認(rèn)開(kāi)啟挂捅,會(huì)隱藏每個(gè)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ù)源與代理方法我都用宏寫(xiě)入YQCommonMacro中闲先。設(shè)置cell的屬性全在- (void)setData方法中實(shí)現(xiàn)。

效果:

Main.png

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

博客地址

jabberyq 自造輪子:YQCommonCell 簡(jiǎn)化表單視圖開(kāi)發(fā)

下載

GitHub:YQCommonCell

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市斥季,隨后出現(xiàn)的幾起案子训桶,更是在濱河造成了極大的恐慌累驮,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舵揭,死亡現(xiàn)場(chǎng)離奇詭異谤专,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)午绳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)置侍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人箱叁,你說(shuō)我怎么就攤上這事墅垮。” “怎么了耕漱?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵算色,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我螟够,道長(zhǎng)灾梦,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任妓笙,我火速辦了婚禮若河,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寞宫。我一直安慰自己萧福,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布辈赋。 她就那樣靜靜地躺著鲫忍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪钥屈。 梳的紋絲不亂的頭發(fā)上悟民,一...
    開(kāi)封第一講書(shū)人閱讀 52,328評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音篷就,去河邊找鬼射亏。 笑死,一個(gè)胖子當(dāng)著我的面吹牛竭业,可吹牛的內(nèi)容都是我干的智润。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼未辆,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼做鹰!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起鼎姐,我...
    開(kāi)封第一講書(shū)人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后炕桨,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體饭尝,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年献宫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钥平。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡姊途,死狀恐怖涉瘾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捷兰,我是刑警寧澤立叛,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站贡茅,受9級(jí)特大地震影響秘蛇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜顶考,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一赁还、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧驹沿,春花似錦艘策、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至梭域,卻和暖如春斑举,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背病涨。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工富玷, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人既穆。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓赎懦,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親幻工。 傳聞我的和親對(duì)象是個(gè)殘疾皇子励两,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子囊颅,類(lèi)似...
    liudhkk閱讀 9,067評(píng)論 3 38
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,675評(píng)論 1 9
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)当悔、插件傅瞻、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,120評(píng)論 4 61
  • 我們?cè)谏弦黄锻ㄟ^(guò)代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識(shí),本文將在上文的基礎(chǔ)上盲憎,利用sto...
    啊世ka閱讀 1,512評(píng)論 2 7
  • 提起馬陵山嗅骄,大家就會(huì)想到孫臏和龐涓的馬陵之戰(zhàn),今兒個(gè)咱不聊這個(gè)饼疙,聊聊一些神神叨叨的事情溺森。 01 黑龍...
    木薇清閱讀 1,915評(píng)論 6 8