iOS 多textField頁(yè)面(tableview)布局,利用model綁定數(shù)據(jù)

相信好多小伙伴都有遇到開(kāi)發(fā)多textField頁(yè)面的時(shí)候,就是一個(gè)頁(yè)面有多個(gè)textField,例如下圖這樣:

QQ20170324-213821.png

這個(gè)是輸入框比較少的,但是我在調(diào)試過(guò)程中,試過(guò)加入多個(gè)textField,也是沒(méi)問(wèn)題的。
言歸正傳,我當(dāng)初看到這個(gè)頁(yè)面是,我有想過(guò)用scrollView,然后上面再一個(gè)個(gè)布局textField,但感覺(jué)這樣比較蠢。。。所以我就用tableView,但是tableView里搞多個(gè)textField的話,問(wèn)題比較多,例如:
1.tableview滑動(dòng),cell在復(fù)用時(shí),導(dǎo)致textField里的舊值會(huì)被刷掉
2.在最后表單提交時(shí),需要把每一個(gè)輸入框的值獲取提交,十分麻煩等等。。。

所以,我在考慮怎么布局時(shí),想到,利用model去綁定數(shù)據(jù),我顯示創(chuàng)建一個(gè)model,用來(lái)綁定數(shù)據(jù):

typedef NS_ENUM(NSUInteger, CreateTableCellType) {
    CreateTableNormalCell,
    CreateTableTFCell,
    CreateTableSeparatorCell,
};

@interface BZCreateTableModel : NSObject
// 名稱(chēng)
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *placeholder;
// 表單對(duì)應(yīng)的字段
@property (nonatomic, copy)NSString *key;
//cell圖片
@property (nonatomic,copy) NSString *imageName;
// cell的類(lèi)型
@property (nonatomic, assign)CreateTableCellType cellType;
@property (nonatomic,assign) TextFieldCellSeparatorType textFieldCellSeparatorType;
@property (nonatomic,assign) BOOL textFieldShowBtn;
@end

其中key就是提交數(shù)據(jù)時(shí)的名字,例如:店鋪名稱(chēng),提交數(shù)據(jù)時(shí),后臺(tái)用Name來(lái)接收,所以店鋪名稱(chēng)的key就是Name。

然后,我在controller里,創(chuàng)建model:

- (void)creatData
{
    BZCreateTableModel *model1 = [BZCreateTableModel new];
    model1.cellType = CreateTableTFCell;
    model1.title = @"店鋪名稱(chēng)";
    model1.placeholder = @"必填/請(qǐng)輸入店鋪名稱(chēng)";
    model1.key = @"Name";
    model1.textFieldCellSeparatorType = TextFieldCellSeparatorTop;
    [self.dataArray addObject:model1];
    
    BZCreateTableModel *model2 = [BZCreateTableModel new];
    model2.cellType = CreateTableTFCell;
    model2.title = @"手機(jī)號(hào)碼";
    model2.placeholder = @"必填/請(qǐng)輸入手機(jī)號(hào)碼";
    model2.key = @"Phone";
    model2.textFieldShowBtn = YES;
    [self.dataArray addObject:model2];
    
    BZCreateTableModel *model3 = [BZCreateTableModel new];
    model3.cellType = CreateTableTFCell;
    model3.title = @"驗(yàn)  證  碼";
    model3.placeholder = @"請(qǐng)輸入手機(jī)短信驗(yàn)證碼";
    model3.key = @"ValidateCode";
    model3.textFieldCellSeparatorType = TextFieldCellSeparatorBomtom;
    [self.dataArray addObject:model3];
    
    BZCreateTableModel *model4 = [BZCreateTableModel new];
    model4.cellType = CreateTableSeparatorCell;
    [self.dataArray addObject:model4];
    
    BZCreateTableModel *model5 = [BZCreateTableModel new];
    model5.cellType = CreateTableTFCell;
    model5.title = @"登錄密碼";
    model5.placeholder = @"密碼6-18位,建議數(shù)字與字母組合";
    model5.key = @"Password";
    model5.textFieldCellSeparatorType = TextFieldCellSeparatorTop;
    [self.dataArray addObject:model5];
    
    BZCreateTableModel *model6 = [BZCreateTableModel new];
    model6.cellType = CreateTableTFCell;
    model6.title = @"推  薦  人";
    model6.placeholder = @"如果沒(méi)有可以不填寫(xiě)";
    model6.key = @"Referrer";
    model6.textFieldCellSeparatorType = TextFieldCellSeparatorBomtom;
    [self.dataArray addObject:model6];
}

這里可以看到,model已經(jīng)指定了各種屬性了,直接在返回cell的方法中就好寫(xiě)好多:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BZCreateTableModel *model = self.dataArray[indexPath.row];
    
    if (model.cellType == CreateTableTFCell) {
        static NSString *cellID = @"TextFieldCellID";
        BZTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[BZTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.textField.delegate = self;
            [cell textFieldAddObserver:self selector:@selector(textFieldValueChange:)];
        }
        cell.createTableModel = model;
        cell.formDict = self.formDict;
        
        return cell;
    }else if (model.cellType == CreateTableSeparatorCell){
        static NSString *cellID = @"SeparatorCellID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = kBackGroundColor;
        }
        return cell;
    }
    
    return [UITableViewCell new];
}

其中,cell.formDict = self.formDict;這個(gè)formDict,其實(shí)就是用來(lái)最后提交數(shù)據(jù)的一個(gè)字典。
cell.createTableModel = model;
cell.formDict = self.formDict;
就是用來(lái)刷新cell數(shù)據(jù),并且把之前textField的舊值賦值上。

------------------------------------------------華麗的分隔線------------------------------------------------

在cell里面,(這是cell里面的方法)監(jiān)聽(tīng)textField輸入:


Paste_Image.png

監(jiān)聽(tīng)輸入時(shí),及時(shí)把textField最新的值賦值給formDict,保持formDict里面的值是最新的:


Paste_Image.png

除以之外,如果你想在controller監(jiān)聽(tīng)輸入框輸入,做好把控的話也是可以的,
可以在cell里面為controller添加輸入框監(jiān)聽(tīng):


Paste_Image.png

然后再cell創(chuàng)建時(shí),添加監(jiān)聽(tīng)就可以:

 [cell textFieldAddObserver:self selector:@selector(textFieldValueChange:)];

在controller里,就可以把輸入框輸入做好把控:

- (void)textFieldValueChange:(NSNotification *)note
{
//    UITextField *textField = note.object;
//    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)textField.superview.superview];
//    BZCreateTableModel *model = self.dataArray[indexPath.row];
    
    NSString *Name = self.formDict[@"Name"];
    NSString *ValidateCode = self.formDict[@"ValidateCode"];
    NSString *Phone = self.formDict[@"Phone"];
    NSString *Password = self.formDict[@"Password"];
    if (Name.length > 0 && [BZFunction validateMobile:Phone] && ValidateCode.length > 0 && [BZFunction validatePassword:Password]) {
        self.registerEnterBtn.enabled = YES;
        self.registerEnterBtn.backgroundColor = kYellowColor;
    }else{
        self.registerEnterBtn.enabled = NO;
        self.registerEnterBtn.backgroundColor = kDisableColor;
    }
    
}

基本輸入框的布局也差不多完畢了。。
還有在返回高度的方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BZCreateTableModel *model = self.dataArray[indexPath.row];
    
    if (model.cellType == CreateTableTFCell) {
        return 49;
    }else if (model.cellType == CreateTableSeparatorCell){
        return 17;
    }
    
    return 44;
}

最后再提交數(shù)據(jù)到服務(wù)器時(shí),直接就可以調(diào)用formDict:

#pragma mark --申請(qǐng)入駐方法
- (void)registerEnterAction
{
    [BZHTTPClient postUrlString:@"PostResist/" withParam:self.formDict withSuccessBlock:^(id data) {
        [self jumpToLoginVCwithIsRegister:YES];
    } withMessageBlock:^(id message) {
        
    } withFailedBlock:^(NSError *error) {
        
    }];
}

最后這里的formDict里面就是所有數(shù)據(jù)框值的一個(gè)字典了,直接提交就OK了?。?!

大概就是這么多了,后面這種寫(xiě)法還可以擴(kuò)展成多類(lèi)型cell的,例如:一個(gè)頁(yè)面里有上傳圖片的,多選擇的,多輸入框的多類(lèi)型cell,利用model綁定數(shù)據(jù)是個(gè)很不錯(cuò)的選擇!??!

最后奉上代碼地址:
https://github.com/JohnRayWong/textFieldModelDemo

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

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

  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類(lèi)似...
    liudhkk閱讀 9,283評(píng)論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,104評(píng)論 4 61
  • 今天上體育課時(shí)發(fā)生了一件事情,本班的同學(xué)和高三的同學(xué)發(fā)生了沖突,因?yàn)槭裁丛蚓筒徽f(shuō)了,就是因?yàn)橐恍┬∈戮桶l(fā)生本不該...
    成敗火閱讀 211評(píng)論 1 0
  • SSO,Single Sign On,也就是單點(diǎn)登錄,保證一個(gè)賬戶(hù)在多個(gè)系統(tǒng)上實(shí)現(xiàn)單一用戶(hù)的登錄現(xiàn)在隨著網(wǎng)站的壯大...
    風(fēng)間影月閱讀 2,148評(píng)論 0 0
  • 一、找到目標(biāo) 什么是威脅? 一一也就是說(shuō)自控力的敵人是什么!知己知彼,百戰(zhàn)不怠!文中用劍齒虎和奶酪蛋糕形象的比喻了...
    獅子座秋秋閱讀 171評(píng)論 0 0

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