前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >史上最全的iOS之访问自定义cell的textField.text的N种方法

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

作者头像
VV木公子
修改2021-02-17 18:02:10
6.7K0
修改2021-02-17 18:02:10
举报
文章被收录于专栏:TechBoxTechBox

前言

问题背景:自定义cell中有一个UITextField类型的子控件。我们经常要在tableView中拿到某个cell内textField的文本内容进行一些操作。比如某些app的注册界面就是以tableView的形式存在的,注册时往往需要注册姓名、昵称、邮箱、地址、联系方式等信息。然后点击注册或者提交,这些信息就会被提交到远程服务器。有人说,注册页面就那么固定的几行cell,没必要搞得那么复杂,完全可以用静态cell实现。但还有一些情况,当前页面的tableView的cell的行数是不确定的(比如当前页面显示多好行cell由上一个页面决定或者由用户决定),这种情况下不太适合使用静态cell。也不能够通过分支语句的方式一一枚举出各个case。所以需要一中通用的动态的方法。那么我们怎么在tableView中准确的拿到每一行cell中textField的text呢?以下我将要分四个方法分别介绍并逐一介绍他们的优缺点,大家可以在开发中根据实际情况有选择的采用不同的方法。 如下图,就是我之前开发的一个app中用xib描述的一个cell,当用户点击“注册”或者“提交”button时候,我需要在控制器中拿到诸如“法人姓名”这一类的信息:

cellWithXib.png

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

  • 通过控制器的textField属性来拿到每一个cell内textField.text
  • 通过系统默认发送的通知来拿到每一个cell内textField.text
  • 通过自定义的通知来拿到每一个cell内textField.text
  • 通过block来拿到每一个cell内textField.text

方法一(方法1请略过)

1.cell的.h文件声明一个IBOutlet的属性,使其和xib描述的cell中的textField进行关联。 1.在tableViewController.m的类扩展中声明为每一个cell的textField都声明一个UITextField类型的属性,一一对应。 2.在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;
}

但是,这个方法还是有一些小问题,因为cell被重用时,存在存在的内容错乱的现象。有人说,因为我们在cellForRowAtIndexPath用一个UITextField属性引用了cell的contentTextfield,我们可以在willDisplayCell:方法中对cell的contentTextField的内容再次配置回来。而事实上,因为cell此时被重用了,所以,我们的tableViewController的那些分别指向每一行cell的UITextField的属性此时也指向了其他行。所以,这个方法对于cell存在重用的情况是不适合的!

以下是方法一的demo地址

方法二(发送系统通知)

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

UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;

1.我们只需要让tableVeiw控制器注册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,如下图:

Snip20160503_2.png

控制器注册通知

// 注册通知
    [[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) {
        NSString *text = textField.text;
        NSInteger row =textField.indexPath.row;
        
        if (text && text.length) {
            [self.contents replaceObjectAtIndex:row withObject:text];
        }
    } else if (textField.indexPath.section == 1) {
        // 同上,请自行脑补
    } else if (textField.indexPath.section == 2) {
        // 同上,请自行脑补
    } else {
        // 同上,请自行脑补
    }
}

切记:对于cell的重用,当在willDisplayCell方法中重新配置cell时候,有if,就必须有else。因为之前屏幕上出现的cell离开屏幕被缓存起来时候,cell上的内容并没有清空,当cell被重用时,系统并不会给我们把cell上之前配置的内容清空掉,所以我们在else中对contentTextField内容进行重新配置或者清空(根据自己的业务场景而定)

以下是方法二的demo地址

方法三(发送自定义通知)

其实方法三和方法二很像,都需要给自定义的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(cellTextFieldDidEndEditing:) 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)cellTextFieldDidEndEditing:(NSNotification *)noti {
    CustomTextField *textField = noti.object;
    if (!textField.indexPath) {
        return;
    }
    
    NSString *userInfoValue = [noti.userInfo objectForKey:@"textFieldText"];
    NSLog(@"text:%@,userInfoValue:%@",textField.text,userInfoValue);
    // 如果涉及到多个section,可以使用二维数组,此处不再赘述
    if (textField.indexPath.section == 0) {
        [self.contents replaceObjectAtIndex:textField.indexPath.row withObject:userInfoValue];

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

切记:对于cell的重用,当在willDisplayCell方法中重新配置cell时候,有if,就必须有else。因为之前屏幕上出现的cell离开屏幕被缓存起来时候,cell上的内容并没有清空,当cell被重用时,系统并不会给我们把cell上之前配置的内容清空掉,所以我们在else中对contentTextField内容进行重新配置或者清空(根据自己的业务场景而定)

以下是方法三的demo地址 方法三相对于方法二的好处在于:方法三发送的是自定义通知,而方法二发送的是系统自带的通知。 因为项目开发中,受项目复杂度影响,难免会出现不同的控制器界面都会有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控制器时,控制器可能会注册多份相同的通知。

方法四(使用block)

1>给cell添加一个block属性,该block属性带有一个NSString *类型的参数。 2>给cell的textField添加target,触发方法的事件是UIControlEventEditingChanged 3>textField触发的方法中调用cell的这个block属性,并把contentTextField.text作为block的参数传进去 4>数据源方法cellForRowAtIndexPath:中对cell的block属性赋值(也就是拿到cell.contentTextField.text) 5>数据源方法willDisplayCell:中对cell重新配置。

给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赋值

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

    TableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0) {
        customCell.block = ^(NSString * text) {
            // 更新数据源
            [weakSelf.contents replaceObjectAtIndex:indexPath.row withObject:text];
        };
    } else if (indexPath.section == 1) {
        // 同上,请自行脑补
    } else {
        // 同上,请自行脑补
    }
    return customCell;
}

在willDisplayCell:方法中对cell进行配置:

- (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) {
        customCell.contentTextField.text = [self.contents objectAtIndex:indexPath.row];
        // 必须有else!
    } else {
        // 切记:对于cell的重用,有if,就必须有else。因为之前屏幕上出现的cell离开屏幕被缓存起来时候,cell上的内容并没有清空,当cell被重用时,系统并不会给我们把cell上之前配置的内容清空掉,所以我们在else中对contentTextField内容进行重新配置或者清空(根据自己的业务场景而定)
        customCell.contentTextField.text = [NSString stringWithFormat:@"第%ld组,第%ld行",indexPath.section,indexPath.row];
    }
}

切记:对于cell的重用,当在willDisplayCell方法中重新配置cell时候,有if,就必须有else。因为之前屏幕上出现的cell离开屏幕被缓存起来时候,cell上的内容并没有清空,当cell被重用时,系统并不会给我们把cell上之前配置的内容清空掉,所以我们在else中对contentTextField内容进行重新配置或者清空(根据自己的业务场景而定)

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

方法五(使用delegate实现)

方法五和方法四很像,只不过方法五采用了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地址 文/VV木公子(简书作者)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.04.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 方法一(方法1请略过)
    • TableViewCell.h文件中的contentTextField引用xib中的textField:
      • 控制器中声明UITextField类型的属性。
        • 数据源方法cellForRowAtIndexPath:中给控制器的UITextField类型属性赋值。
        • 方法二(发送系统通知)
          • 自定义UITextField
            • 控制器注册通知
              • 给自定义的textField的indexPath属性赋值
                • 监听到通知后调用的方法
                • 方法三(发送自定义通知)
                  • 给CustomTextField添加indexPath属性
                    • 给自定义cell添加CustomTextField类型contentTextField属性
                      • 遵守协议,设置delegate,实现协议方法
                      • 方法四(使用block)
                      • 方法五(使用delegate实现)
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档