前言
由于寫(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)目中的效果圖:
內(nèi)部結(jié)構(gòu)
其實(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.
效果:
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.
效果:
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;
效果:
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)
效果:
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ù)雜界面的繪制。
效果:
其他屬性展示
// 分割線 對(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)的是tableview
的Section
蛮瞄,因此它的屬性都與Section
相關(guān)。
其中需要注意的是hiddenLastRowBottomLine
屬性谆扎,默認(rèn)開(kāi)啟挂捅,會(huì)隱藏每個(gè)Section
最后一行cell
的分割線。
效果:
如何使用
@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)。
效果:
至于其他效果的實(shí)現(xiàn)无蜂,可下載demo了解伺糠。
博客地址
jabberyq 自造輪子:YQCommonCell 簡(jiǎn)化表單視圖開(kāi)發(fā)