通用TableView

前言:
編寫iOS App的時候經(jīng)常會使用tableView來配置一些基礎(chǔ)頁面。

作者以前寫iOS的時候發(fā)現(xiàn)每次要寫一個列表很麻煩,因為本來有寫java后臺的,大多都是配置化,就想了一下,能不能封裝一個tableview,配置一下就呈現(xiàn)你要的頁面,支持自定義cell,所以就有了這個例子。
demo項目

這是項目的使用構(gòu)建一個頁面代碼:

//
//  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:@"自定義就實現(xiàn)我的協(xié)議就好",
                          HeaderHeight : @(30),
                          RowContent :@[
                                  @{
                                      ExtraInfo    : @"實際開放中你可以傳進(jìn)去用戶的id,在cell里面實現(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:@"加不加點擊事件由你"];
}

- (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:@"開"];
    }else {
        [self showAlertWithString:@"關(guān)"];
    }
}

- (void)touchShop{
    [self showAlertWithString:@"點擊了商城"];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];//關(guān)閉鍵盤
    [self showAlertWithString:self.cellText];
    return YES;
}

//其實你寫個代理傳出來,不過我比較懶就直接用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

效果頁面:

image.png
image.png

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

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容