通用TableView

前言:
編寫(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è)面:

image.png
image.png

項(xiàng)目結(jié)構(gòu):

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市毫玖,隨后出現(xiàn)的幾起案子掀虎,更是在濱河造成了極大的恐慌,老刑警劉巖付枫,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件烹玉,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡阐滩,警方通過(guò)查閱死者的電腦和手機(jī)二打,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)叶眉,“玉大人址儒,你說(shuō)我怎么就攤上這事⌒聘恚” “怎么了莲趣?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)饱溢。 經(jīng)常有香客問(wèn)我喧伞,道長(zhǎng),這世上最難降的妖魔是什么绩郎? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任潘鲫,我火速辦了婚禮,結(jié)果婚禮上肋杖,老公的妹妹穿的比我還像新娘溉仑。我一直安慰自己,他們只是感情好状植,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布浊竟。 她就那樣靜靜地躺著,像睡著了一般津畸。 火紅的嫁衣襯著肌膚如雪振定。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,258評(píng)論 1 300
  • 那天肉拓,我揣著相機(jī)與錄音后频,去河邊找鬼。 笑死暖途,一個(gè)胖子當(dāng)著我的面吹牛卑惜,可吹牛的內(nèi)容都是我干的膏执。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼残揉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼胧后!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起抱环,我...
    開(kāi)封第一講書(shū)人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤壳快,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后镇草,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體眶痰,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年梯啤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了竖伯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡因宇,死狀恐怖七婴,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情察滑,我是刑警寧澤打厘,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站贺辰,受9級(jí)特大地震影響户盯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饲化,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一莽鸭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吃靠,春花似錦硫眨、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至夕冲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間裂逐,已是汗流浹背歹鱼。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留卜高,地道東北人弥姻。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓南片,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親庭敦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子疼进,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件秧廉、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,098評(píng)論 4 62
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,095評(píng)論 25 707
  • 其實(shí)小武是一個(gè)哲學(xué)家伞广,這我是知道的,不知道別人怎么看疼电,認(rèn)為他是數(shù)學(xué)小戰(zhàn)士嚼锄,亦或者是黃賭毒之王,但我認(rèn)為他首先是一個(gè)...
    RRRocker閱讀 380評(píng)論 0 0
  • 呆呆地坐著蔽豺, 沒(méi)有歡喜 只是呆呆地 有東西糊住了眼眶 黏答答像是水的尸體 似有萬(wàn)語(yǔ)千言 卻又只憋出一聲嘆息
    王不煩閱讀 160評(píng)論 0 0
  • NSAttributedString 可以非常方便的實(shí)現(xiàn)文字排版和圖文混排功能. 共有21種效果(API), 本文...
    嘖嘖嘖_野獸閱讀 6,727評(píng)論 0 9