TableView 单元格中的 UITextField

Posted

技术标签:

【中文标题】TableView 单元格中的 UITextField【英文标题】:UITextField in TableView Cells 【发布时间】:2013-05-10 08:35:45 【问题描述】:

我正在尝试将 UITextField 动态添加到 TableView 这是我的代码

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.tag=temp+indexPath.row+1;

[cell.contentView addSubview:textField];

问题是每次显示一个单元格时,它都会在同一位置创建一个 newTextField,因此它们重叠,我无法在另一个 TextField 中进行编辑,并且它们具有相同的标签.. 我只想为每个单元格创建一次 TextField,即使它会再次显示

【问题讨论】:

检查是否 (cell==nil) 然后使用 textfiled 创建单元格,否则将重用单元格以避免多次创建 textfiled。 @sugan.s 如果一个单元格被注册为重用,它不会是 nil。 是的,第一次加载 tableview 时,时间单元格将为零。@Anupdas @sugan.s 你能支持你的说法吗?阅读 – dequeueReusableCellWithIdentifier:forIndexPath: 的文档。此方法总是返回一个有效的单元格。 【参考方案1】:

您需要创建自定义 UITableViewCell 类

TableViewCellWithTextField.h

#import <UIKit/UIKit.h>

@interface TableViewCellWithTextField : UITableViewCell
@property(nonatomic,strong) UITextField *textField;
@end

TableViewCellWithTextField.m

@implementation TableViewCellWithTextField

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
        [self addSubview:_textField];
        // Initialization code
    
    return self;

@end

然后你可以像这样使用你的文本字段:

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

    NSString * cellIdentifier= @"Cell"
    TableViewCellWithTextField *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if(!cell)
    
        cell = [[TableViewCellWithTextField alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
    cell.textField.tag = temp + indexPath.row + 1;

【讨论】:

我认为这会有所不同,因为每次你显示单元格时都会调用 cellForRowAtIndexPath,所以同一个单元格将被多次调用,我们将遇到同样的问题 有所作为。新的 TableViewCellWithTextFields 只会在没有可以出队的情况下才会被实例化,并且 UITextField 只会在第一次实例化时添加到 Cell 中。【参考方案2】:

如您所知,您的使用方式会导致内存泄漏。因此,您可以创建一个自定义单元格,该单元格具有 textview 作为属性并在 cellForRowAtIndexPath: 方法中访问 textview。每个单元格只有一个文本视图,可以通过属性访问,就像访问单元格的标签一样。

另一种方法是使用 cellForRowAtIndexPath: 方法中的标签访问文本视图,而不是每次都创建。

【讨论】:

【参考方案3】:

您可能应该为所有文本字段(无论它们位于哪个表格视图单元格中)赋予相同的标签 TEXT_FIELD_TAG

#define TEXT_FIELD_TAG 1000

每次调用 tableView:cellForRowAtIndexPath: 时,你应该检查一个带有 TEXT_FIELD_TAG 的子视图是否已经存在,像这样:

UITextField *textField = [cell.contentView viewWithTag: TEXT_FIELD_TAG];
if(!textField)
    textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
    textField.tag=temp+indexPath.row+1;
    [cell.contentView addSubview:textField];

如果 textField = nil 那么您需要创建一个新的 UITextField 并将其添加到内容视图中。

【讨论】:

那是真正的工作人员,非常感谢你的灵感,但它需要一些改进来赋予每个 TextField 唯一标签,以便我以后可以在它们中获得价值......【参考方案4】:

我终于在你的帮助下终于发现了

if (![cell.contentView viewWithTag:temp+indexPath.row+1 ]) 
     UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];

    textField.tag=temp+indexPath.row+1;

    textField.delegate=self;

    textField.autocorrectionType = UITextAutocorrectionTypeNo;
   // textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    [cell.contentView addSubview:textField];

所以在这里我将保证每个单元格只创建一次 UITextField...


【讨论】:

如果您对其中一个答案感到满意,请接受。 @Ulugbek 的 UITableViewCell 子类化解决方案更加优雅和可重用...

以上是关于TableView 单元格中的 UITextField的主要内容,如果未能解决你的问题,请参考以下文章

多个tableView单元格中的多个collectionView

TableView 交互阻止单元格中的动画

TableView 单元格中的 MVVM

Tableview 禁用单元格中的用户交互

在tableView中选择多个单元格并将另一个tableView单元格中的选定单元格显示为标签?

reloadData后无法单击tableView单元格中的按钮