前言:
編寫(xiě)iOS App的時(shí)候經(jīng)常會(huì)使用tableView來(lái)配置一些基礎(chǔ)頁(yè)面读存。
作者以前寫(xiě)iOS的時(shí)候發(fā)現(xiàn)每次要寫(xiě)一個(gè)列表很麻煩,因?yàn)楸緛?lái)有寫(xiě)java后臺(tái)的医舆,大多都是配置化炕柔,就想了一下,能不能封裝一個(gè)tableview购撼,配置一下就呈現(xiàn)你要的頁(yè)面跪削,支持自定義cell谴仙,所以就有了這個(gè)例子。
demo項(xiàng)目
這是項(xiàng)目的使用構(gòu)建一個(gè)頁(yè)面代碼:
//
// ViewController.m
// CommonTableView
//
// Created by zj on 16/10/15.
// Copyright ? 2016年 xuezhijian. All rights reserved.
//
#import "ViewController.h"
#import "CommonTableDelegate.h"
#import "CommonTableData.h"
typedef NS_ENUM(NSInteger, UserGender) {
/**
* 未知性別
*/
UserGenderUnknown,
/**
* 性別男
*/
UserGenderMale,
/**
* 性別女
*/
UserGenderFemale,
};
@interface ViewController ()
@property(nonatomic,strong) CommonTableDelegate *delegator;
@property(nonatomic,strong) NSArray *data;
@property(nonatomic,assign) UserGender selectedGender;
@property(nonatomic,strong) NSString *cellText;//輸入框的文本內(nèi)容
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
[self refresh];
}
- (void)setupUI{
__weak typeof(self) weakSelf = self;
_delegator = [[CommonTableDelegate alloc] initWithTableData:^NSArray *{
return weakSelf.data;
}];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_tableView];
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_tableView.delegate = self.delegator;
_tableView.dataSource = self.delegator;
_tableView.showsVerticalScrollIndicator = NO;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTableView:)];
[_tableView addGestureRecognizer:tapGesture];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)buildData{
NSArray *data = @[
@{
HeaderTitle :@"這組是默認(rèn)樣式",
HeaderHeight : @(30),
RowContent :@[
@{
Title : @"主標(biāo)題",
DetailTitle : @"描述 ",
CellAction : @"touchCell1",
ShowAccessory : @(YES)
},
@{
Title : @"主標(biāo)題",
RowHeight : @(60),
ShowAccessory : @(YES)
}
],
FooterTitle : @"",
FooterHeight : @(10)
},
@{
HeaderTitle:@"這組是公共樣式的CommonTextFieldCell",
HeaderHeight : @(30),
RowContent :@[
@{
Title : @"文本cell",
DetailTitle : @"輸入些文字試試",
CellClass : @"CommonTextFieldCell",
ExtraInfo : @(UIReturnKeyDone),//設(shè)置鍵盤確認(rèn)按鈕類型
}
],
FooterTitle : @"",
FooterHeight : @(10)
},
@{
HeaderTitle :@"這組是公共樣式的CheckBoxCell",
HeaderHeight : @(30),
RowContent :@[
@{
Title : @"男",
CellClass : @"CommonCheckBoxCell",
CellAction : @"onTouchMaleCell:",
ExtraInfo : @(self.selectedGender == UserGenderMale),
ForbidSelect : @(YES),
},
@{
Title : @"女",
CellClass : @"CommonCheckBoxCell",
CellAction : @"onTouchFemaleCell:",
ExtraInfo : @(self.selectedGender == UserGenderFemale),
ForbidSelect : @(YES),
},
@{
Title : @"未知",
CellClass : @"CommonCheckBoxCell",
CellAction : @"onTouchUnkownGenderCell:",
ExtraInfo : @(self.selectedGender == UserGenderUnknown),
ForbidSelect : @(YES),
}
],
FooterTitle : @"",
FooterHeight : @(10)
},
@{
HeaderTitle :@"這組是公共樣式的CommonSwitchCell",
HeaderHeight : @(30),
RowContent :@[
@{
Title : @"推送通知",
CellClass : @"CommonSwitchCell",
CellAction : @"switchChange:",
ExtraInfo : @(YES),
ForbidSelect : @(YES)
}
],
FooterTitle : @"",
FooterHeight : @(10)
},
@{
HeaderTitle:@"這組是公共樣式的CommonFunctionCell,帶圖標(biāo)",
HeaderHeight : @(30),
RowContent :@[
@{
Title : @"我的商城",
CellClass : @"CommonFunctionCell",
ExtraInfo : [UIImage imageNamed:@"myCenter_shop"],
CellAction : @"touchShop",
ShowAccessory : @(YES)
}
],
FooterTitle : @"",
FooterHeight : @(10)
},
@{
HeaderTitle:@"自定義就實(shí)現(xiàn)我的協(xié)議就好",
HeaderHeight : @(30),
RowContent :@[
@{
ExtraInfo : @"實(shí)際開(kāi)放中你可以傳進(jìn)去用戶的id碾盐,在cell里面實(shí)現(xiàn)邏輯",
CellClass : @"UserCardPortraitCell",
RowHeight : @(90)
}
],
FooterTitle : @"",
FooterHeight : @(30)
},
];
self.data = [CommonTableSection sectionsWithData:data];
}
- (void)refresh{
[self buildData];
[self.tableView reloadData];
}
#pragma mark - Action
- (void)touchCell1
{
[self showAlertWithString:@"加不加點(diǎn)擊事件由你"];
}
- (void)onTouchMaleCell:(id)sender{
self.selectedGender = UserGenderMale;
[self refresh];
}
- (void)onTouchFemaleCell:(id)sender{
self.selectedGender = UserGenderFemale;
[self refresh];
}
- (void)onTouchUnkownGenderCell:(id)sender{
self.selectedGender = UserGenderUnknown;
[self refresh];
}
- (void)switchChange:(id) sender{
BOOL isButtonOn = [(UISwitch*)sender isOn];
if (isButtonOn) {
[self showAlertWithString:@"開(kāi)"];
}else {
[self showAlertWithString:@"關(guān)"];
}
}
- (void)touchShop{
[self showAlertWithString:@"點(diǎn)擊了商城"];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];//關(guān)閉鍵盤
[self showAlertWithString:self.cellText];
return YES;
}
//其實(shí)你寫(xiě)個(gè)代理傳出來(lái)晃跺,不過(guò)我比較懶就直接用NSNotificationCenter
- (void)onTextFieldChanged:(NSNotification *)notification{
UITextField *textField = notification.object;
self.cellText = textField.text;
}
#pragma mark -Private
- (void)showAlertWithString:(NSString *)text
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"cell"message:text preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定"style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - GestureRecognizer
- (void)tapTableView:(UITapGestureRecognizer *)recognizer{
[self.view endEditing:YES];
}
@end
效果頁(yè)面:
項(xiàng)目結(jié)構(gòu):