iOS之访问自定义cell的textField.text的N种方法

前言

问题背景:

自定义cell中有一个UITextField类型的子控件。我们经常要在tableView中拿到某个cell内textField的文本内容进行一些操作。比如某些app的注册界面就是以tableView的形式存在的,注册时往往需要注册姓名、昵称、邮箱、地址、联系方式等信息。然后点击注册或者提交,这些信息就会被提交到远程服务器。那么我们怎么在tableView中准确的拿到每一行cell中textField的text呢?以下我将要分四个方法分别介绍并逐一介绍他们的优缺点,大家可以在开发中根据实际情况有选择的采用不同的方法。如下图,就是我之前开发的一个app中用xib描述的一个cell,当用户点击“注册”或者“提交”button时候,我需要在控制器中拿到诸如“法人姓名”这一类的信息:

四个方法告诉你如何在tableView中拿到每一个cell中的textField.text
  • 四个方法分别如下:

    • 通过控制器的textField属性来拿到每一个cell内textField.text

    • 通过系统默认发送的通知来拿到每一个cell内textField.text

    • 通过自定义的通知来拿到每一个cell内textField.text

    • 通过block来拿到每一个cell内textField.text


方法一

  • 1 .cell的.h文件声明一个IBOutlet的属性,使其和xib描述的cell中的textField进行关联。

  • 2 .在tableViewController.m的类扩展中声明为每一个cell的textField都声明一个UITextField类型的属性,一一对应。

  • 3 .在cellForRowAtIndexPath:数据源方法中给控制器的每个UITextField类型属性赋值为cell.textField。

TableViewCell.h文件中的contentTextField引用xib中的textField:

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell
/**
 *  cell的标题
 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

/**
 *  cell的文本框
 */
@property (weak, nonatomic) IBOutlet UITextField *contentTextField;
@end

控制器中声明UITextField类型的属性。

@interface YQBInfoViewController ()
/**
 *  标题
 */
@property(nonatomic, strong) NSArray *titles;
/**
 *  占位文字
 */
@property(nonatomic, strong) NSArray *placeHolders;

/**
 *  姓名
 */
@property(nonatomic, weak) UITextField *nameTextField;

/**
 *  年龄
 */
@property(nonatomic, weak) UITextField *ageTextField;

/**
 *  地址
 */
@property(nonatomic, weak) UITextField *addressTextField;
@end

数据源方法cellForRowAtIndexPath:中给控制器的UITextField类型属性赋值。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 在这里把每个cell的textField 赋值给 事先声明好的UITextField类型的属性
    // 以后直接操作控制器的这些属性就可以拿到每个textField的值
    switch (indexPath.row) {
        case 0:
            // 姓名
            self.nameTextField = cell.contentTextField;
            break;
        case 1:
            // 年龄
            self.ageTextField = cell.contentTextField;
            break;
        case 2:
            // 地址
            self.addressTextField = cell.contentTextField;
            break;
        default:
            break;
    }

    return cell;
}

以下是方法一的demo地址:https://github.com


方法二

我们知道UITextField内容改变时会发送通知。与UITextField相关的通知有三个,如下:

UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
  • 1.我们只需要让tableview控制器注册
-UITextFieldTextDidChangeNotification/UITextFieldTextDidEndEditingNotification 通知。
  • 2.在数据源方法cellForRowAtIndexPath:中对cell.textField.tag赋值为indexPath.row。这样就可以区分每一行的textField。

  • 3.然后在监听到通知后调用的方法中,根据textField.tag拿到textField的内容。

但是,问题来了,`如果tableView是grouped样式的呢?这样就有可能存在两个textField具有相同的tag!所以,以上提供的思路只适用于plained样式的tableView。grouped样式的tableView建议用下面的方法。

解决方法:
  • 自定义textField,给textField添加NSIndexPath类型的属性indexPath。我们这次给textField的indexPath赋值而不是tag。这样就可以在监听到通知后调用的方法中,根据indexPath来区分不同的section和row。

自定义UITextField

#import <UIKit/UIKit.h>

@interface CustomTextField : UITextField
/**
 *  indexPath属性用于区分不同行cell
 */
@property (strong, nonatomic) NSIndexPath *indexPath;
@end
注意:
  • 如果你自定义的cell是用xib描述的,不要忘记给cell的textField指定类型为你自定义的textField,此例中我自定义的是CustomTextField,如下图:

控制器注册通知
// 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentTextFieldDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
给自定义的textField的indexPath属性赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    AliyunSalesUnifiedEditCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    // 如果不止一个section,那么传递indexPath.row有可能冲突
    // cell.contentTextField.tag = indexPath.row;

    // 所以传递indexPath,相当于把section也传递给contentTextField
    cell.contentTextField.indexPath = indexPath;
    return cell;
}
监听到通知后调用的方法
// 在这个方法中,我们就可以通过自定义textField的indexPath属性区分不同行的cell,然后拿到textField.text
- (void)contentTextFieldDidEndEditing:(NSNotification *)noti
{
    CustomTextField *textField = noti.object;
    if (textField.indexPath.section == 0) {
        switch (textField.indexPath.row) {
            case 0: // 名称
                self.name = textField.text;
                break;
            case 1: // 年龄
                self.age = textField.text;
                break;
            case 2: // 地址
                self.address = textField.text;
                break;

            default:
                break;
        }
    } else if (textField.indexPath.section == 1) {
        // 同上,请自行脑补
    } else if (textField.indexPath.section == 2) {
        // 同上,请自行脑补
    } else {
        // 同上,请自行脑补
    }
}

以下是方法二的demo地址:https://github.com/textFieldCell2


方法三

其实方法三和方法二很像,都需要给自定义的textField添加indexPath属性,也需要发送通知,然后在通知中心对这个通知注册监听。区别在于,方法二发送的是系统自带的通知

UITextFieldTextDidEndEditingNotification

而方法三将要发送自定义通知。

  • 1>给CustomTextField添加indexPath属性。

  • 2>给自定义cell添加CustomTextField类型contentTextField属性。

  • 3>cell遵守UITextFieldDelegate协议,成为textField属性的delegate。

  • 4>cell实现协议方法-textFieldDidEndEditing:(UITextField *)textField

  • 5>textFieldDidEndEditing:协议方法中发送一个自定义的通知,并且把textField.text通过userInfo字典发出去。

具体实现代码:
给CustomTextField添加indexPath属性
#import <UIKit/UIKit.h>

@interface CustomTextField : UITextField
/**
 *  indexPath属性用于区分不同的cell
 */
@property (strong, nonatomic) NSIndexPath *indexPath;
@end
给自定义cell添加CustomTextField类型contentTextField属性
#import <UIKit/UIKit.h>
@class CustomTextField;

@interface TableViewCell : UITableViewCell
/**
 *  cell的标题
 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

/**
 *  cell的文本框
 */
@property (weak, nonatomic) IBOutlet CustomTextField *contentTextField;

@end
遵守协议,设置delegate,实现协议方法
#import "TableViewCell.h"
#import "CustomTextField.h"

@interface TableViewCell ()<UITextFieldDelegate>

@end

@implementation TableViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentTextField.delegate = self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 使contentTextField聚焦变成第一响应者
    [self.contentTextField becomeFirstResponder];

}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSDictionary *userInfo = @{
                               @"textFieldText":self.contentTextField.text
                               };
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CustomTextFieldDidEndEditingNotification" object:self.contentTextField userInfo:userInfo];
}
  • 6>控制器注册并监听该通知

  • 7>在监听到通知的方法中通过userInfo拿到textField的text属性

  • 8>- (void)viewWillDisappear:(BOOL)animated方法中移除监听

  • 9>完毕

注册通知
// 如果不能保证控制器的dealloc方法肯定会被调用,不要在viewDidLoad方法中注册通知。
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // 注意:此处监听的通知是:UITextFieldTextDidEndEditingNotification,textField结束编辑发送的通知,textField结束编辑时才会发送这个通知。
    // 想实时监听textField的内容的变化,你也可以注册这个通知:UITextFieldTextDidChangeNotification,textField值改变就会发送的通知。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aLiYunTextFieldDidEndEditing:) name:@"CustomTextFieldDidEndEditingNotification" object:nil];

    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentTextFieldDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
}
移除通知
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 在这个方法里移除通知,因为:
    // 防止控制器被强引用导致-dealloc方法没有调用
    // 其他界面也有textField,其他界面的textField也会发送同样的通知,导致频繁的调用监听到通知的方法,而这些通知是这个界面不需要的,所以在视图将要消失的时候移除通知 同样,在视图将要显示的时候注册通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CustomTextFieldDidEndEditingNotification" object:nil];
}
接收到通知回调方法
// 接收到注册监听的通知后调用
- (void)aLiYunTextFieldDidEndEditing:(NSNotification *)noti
{
    CustomTextField *textField = noti.object;

    if (!textField.indexPath) {
        return;
    }

    NSString *userInfoValue = [noti.userInfo objectForKey:@"textFieldText"];
    NSLog(@"text:%@,userInfoValue:%@",textField.text,userInfoValue);
    if (textField.indexPath.section == 0) {
        switch (textField.indexPath.row) {
            case 0: // 名称
                self.name = textField.text;
                break;
            case 1: // 年龄
                self.age = textField.text;
                break;
            case 2: // 地址
                self.address = textField.text;
                break;

            default:
                break;
        }
    } else if (textField.indexPath.section == 1) {
        // 同上,请自行脑补
    } else if (textField.indexPath.section == 2) {
        // 同上,请自行脑补
    } else {
        // 同上,请自行脑补
    }
}

以下是方法三的demo地址:https://github.com/textFieldCell3

方法三相对于方法二的好处在于:方法三发送的是自定义通知,而方法二发送的是系统自带的通知。

因为项目开发中,受项目复杂度影响,难免会出现不同的控制器界面都会有UITextField类型(或者其子类型)的对象而没有释放,当textField开始编辑、内容发生改变、结束编辑时,都会发送相同的通知。此时如果我们采用监听系统自带的通知的方法,就有可能监听到我们不需要的改变从而影响了业务数据。

举个例子:A和B控制器都是UITableViewController类型的对象,A、B控制器界面上都有 UITextField 类型(或者其子类型)的子控件。并且A、B控制器都注册了系统自带的UITextField的通知UITextFieldTextDidChangeNotification ,且监听到通知后都会调用各自的 contentTextFieldTextDidChange:方法。当A控制器pushB控制器后,我们在B控制器界面上的TextField编辑内容,A控制器此时也监听了该通知,所以,A控制器上的contentTextFieldTextDidChange:方法也会被调用。这是我们不想得到的,所以,采用自定义通知的方法可以避免这一问题。

当然,我们也可以在viewWillAppear:方法中注册通知,然后在viewWillDisAppear:方法中移除通知,这样同样可以避免这一为题。
另外,值得提醒的是,如果我们不能保证控制器被pop时肯定会调用dealloc方法,那么建议在控制器的viewWillDisAppear:方法中移除通知,而非dealloc方法中移除。否则,用户反复push、pop控制器时,控制器可能会注册多份相同的通知。


方法四

  • 1>给cell添加一个block属性,该block属性带有一个NSString *类型的参数。

  • 2>给cell的textField添加target,触发方法的事件是UIControlEventEditingChanged

  • 3>textField触发的方法中调用cell的这个block属性,并把contentTextField.text作为block的参数传进去

  • 4>数据源方法cellForRowAtIndexPath:中对cell的block属性赋值(也就是拿到cell.contentTextField.text)

给cell添加一个block属性
#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell
/**
 *  block 参数为textField.text
 */

@property (copy, nonatomic) void(^block)(NSString *);
/**
 *  cell的标题
 */

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

/**
 *  cell的文本框
 */
@property (weak, nonatomic) IBOutlet UITextField *contentTextField;

@end
给textField addTarget
在事件触发方法中调用block并传递参数
#import "TableViewCell.h"

@interface TableViewCell ()

@end

@implementation TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.contentTextField addTarget:self action:@selector(textfieldTextDidChange:) forControlEvents:UIControlEventEditingChanged];
// 注意:不是 UIControlEventValueChanged
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.contentTextField becomeFirstResponder];
}

#pragma mark - private method
- (void)textfieldTextDidChange:(UITextField *)textField
{
    self.block(self.contentTextField.text);
}
@end
在cellforRowAtIndexPath:方法中为每个cell的block赋值
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *customCell = (TableViewCell *)cell;
    customCell.titleLabel.text = self.titles[indexPath.row];
    customCell.contentTextField.placeholder = self.placeHolders[indexPath.row];

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                customCell.block = ^(NSString *text){
                    self.name = text;
                };
                break;
            }
            case 1:
            {
                customCell.block = ^(NSString *text){
                    self.age = text;
                };
                break;
            }
            case 2:
            {
                customCell.block = ^(NSString *text){
                    self.address = text;
                };
                break;
            }
            default:
                break;
        }
    } else if (indexPath.section == 1) {
        // 同上,请自行脑补
    } else {
        // 同上,请自行脑补
    }
}

以下是方法四的demo地址:https://github.com/textFieldCell4

方法四相对于方法二和方法三的好处在于:方法四没有采用通知的方式来获取contentTextField.text,而是采用灵活的block。并且方法四也无需自定义textField。


方法五

  • 方法五和方法四很像,只不过方法五采用了delegate方式,更好的做到了解耦。

    • 0>和方法二、方法三一样,cell的textField属性都需要使用自定义类型,因为我们需要给textField绑定indexPath属性。
      1>给cell制定一份协议,协议中有一个方法,带有两个参数,一个是textField的text,另一个是indexPath。同时给cell添加一个delegate属性。

    • 2>给cell的textField添加target,触发方法的事件是UIControlEventEditingChanged

    • 3>textField触发的方法中调用cell的协议方法,并把contentTextField.indexPath作为协议方法的参数传进去

    • 4>数据源方法cellForRowAtIndexPath:中对cell的indexPath赋值为当前的indexPath。对cell的delegate赋值为当前controller

    • 5>控制器实现cell的协议方法,在协议方法里可以拿到textField的文本。

    • 6>在tableView:willDisplayCell:forRowAtIndexPath:方法内刷新tableView。

#import <UIKit/UIKit.h>
@class CustomTextField;
@protocol CustomCellCellDelegate <NSObject>

@required
// cell 的contentTextField的文本发生改变时调用
- (void)contentDidChanged:(NSString *)text forIndexPath:(NSIndexPath *)indexPath;

@end
@interface TableViewCell : UITableViewCell
/**
 *  cell的标题
 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

/**
 *  cell的文本框
 */
@property (weak, nonatomic) IBOutlet CustomTextField *contentTextField;
/**
 *  delegate
 */
@property (weak, nonatomic) id<CustomCellCellDelegate> delegate;
cell.m文件
- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.contentTextField addTarget:self action:@selector(contentDidChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)contentDidChanged:(id)sender {
    // 调用代理方法,告诉代理,哪一行的文本发生了改变
    if (self.delegate && [self.delegate respondsToSelector:@selector(contentDidChanged:forIndexPath:)]) {
        [self.delegate contentDidChanged:self.contentTextField.text forIndexPath:self.contentTextField.indexPath];
    }
}
controller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.contentTextField.indexPath = indexPath;
    cell.delegate = self;
    return cell;
}
// cell的代理方法中拿到text进行保存
- (void)contentDidChanged:(NSString *)text forIndexPath:(NSIndexPath *)indexPath {
    [self.contents replaceObjectAtIndex:indexPath.row withObject:text];
}
// 更新UI
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *customCell = (TableViewCell *)cell;
    customCell.titleLabel.text = self.titles[indexPath.row];
    customCell.contentTextField.placeholder = self.placeHolders[indexPath.row];
    customCell.contentTextField.text = self.contents[indexPath.row];
}

以下是方法五的demo地址:https://github.com/TextFieldCell5

文/VV木公子(简书作者)
原文链接:http://www.jianshu.com
著作权归作者所有

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值