前言
由于寫了太多次的設(shè)置界面、個人中心界面等,已經(jīng)寫到麻木。漸漸意識到,或許可以把這些類似的TableViewCell封裝成一種,通過簡單的方法去創(chuàng)建以及使用,這樣就可以大大減少勞動力啦。
在這篇文章之前,我已經(jīng)寫了一篇介紹YQCommonCell的文章,但是因?yàn)楦碌拇螖?shù)過多內(nèi)容雜亂,我決定重新寫一篇。
項(xiàng)目中的使用效果
很榮幸,在公司的項(xiàng)目中,已經(jīng)使用了這個框架。的確,在開發(fā)表單視圖上節(jié)約了大量時間。下面是兩個項(xiàng)目中的效果圖:
內(nèi)部結(jié)構(gòu)

其實(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.
效果:

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.
效果:

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;
效果:

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)
效果:

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ù)雜界面的繪制。
效果:

其他屬性展示
// 分割線 對應(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)的是tableview的Section,因此它的屬性都與Section相關(guān)。
其中需要注意的是hiddenLastRowBottomLine屬性,默認(rèn)開啟,會隱藏每個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ù)源與代理方法我都用宏寫入YQCommonMacro中。設(shè)置cell的屬性全在- (void)setData方法中實(shí)現(xiàn)。
效果:

至于其他效果的實(shí)現(xiàn),可下載demo了解。
博客地址
jabberyq 自造輪子:YQCommonCell 簡化表單視圖開發(fā)