【问题标题】:UITextField in UITableViewCell HelpUITableViewCell 帮助中的 UITextField
【发布时间】:2011-06-01 21:04:59
【问题描述】:

我已经在互联网上搜寻了一个好的教程或发布了关于在每个单元格中填充 UITextField 以进行数据输入的 UITableView。

我想在滚动时跟踪每个 UITextField 和其中写入的文本。 tableView 将被分割。我一直在使用自定义 UITableViewCell,但我愿意接受任何方法。

另外,是否可以将 textFields 用作 ivars?

如果有人能指出我正确的方向,将不胜感激。

提前谢谢你!

【问题讨论】:

标签: iphone ipad uitableview ios uitextfield


【解决方案1】:

要解决您的问题,您必须维护一个数组,其中包含一些(添加到所有单元格的文本字段的数量)的对象。

在创建该数组时,您需要向该数组添加空的 NSString 对象。每次加载单元格时,您都必须将受尊重的对象替换为受尊重的文本字段。

检查以下代码。

- (void)viewDidLoad{
    textFieldValuesArray = [[NSMutableArray alloc]init];
    for(int i=0; i<numberofRows*numberofSections; i++){
        [textFieldValuesArray addObject:@""];
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return numberofSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return numberofRows;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

     CustomTextField *tf = [[CustomTextField alloc] initWithFrame:CGRectMake(5,5,290,34)];
     tf.tag = 1;
     [cell.contentView addSubView:tf];
     [tf release];
    }
    CustomTextField *tf = (CustomTextField*)[cell viewWithTag:1];
    tf.index = numberofSections*indexPath.section+indexPath.row;
    tf.text = [textFieldValuesArray objectAtIndex:tf.index];

    return cell;
    }

- (void)textFieldDidEndEditing:(UITextField *)textField{

    int index = textField.index;
    [textFieldValuesArray replaceObjectAtIndex:index withObject:textField.text];
}

【讨论】:

  • 如果我在 cellForRow 中使用常规 switch 语句,这是否适用于分段的 tableView?
  • 这也适用于分段表格视图,但我们需要维护一定的索引。我将更改代码检查。
  • "cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];" ??
【解决方案2】:

首先,你必须明白 UITableViewCell 和 UITextField 只是视图,它们不应该保存数据,它们只是应该显示它们并允许用户与它们交互:数据应该保持存储在控制器中表格视图。

您必须记住,UITableView 允许您出于性能目的重用 UITableViewCell 实例:屏幕上显示的实际上是 UITableView 保留在那里的唯一子视图。这意味着您将重用一个已包含文本字段的单元格,并直接在该字段上设置文本。当用户点击该字段时,它会对其进行编辑,当用户完成时,您必须从中取回值。

最快的方法是使用 Satya 提出的方法,即构建普通的 UITableViewCell 并插入 UITextField(不需要 CustomTextField 类......)。该标签将允许您轻松返回文本字段...但是您必须设置您的文本字段,以便在表格视图调整大小或同一单元格中的标签更改时它的行为正常。

最简洁的方法是继承 UITableViewCell 并设置标签和文本字段的布局,您可以将文本字段作为自定义子类的属性提供。

【讨论】:

    【解决方案3】:

    我在表格视图中使用文本字段进行数据输入。 我在一个名为 Utility 的单独类中自定义了 UITextField 类:

    在 Utility.h 中

     @interface CustomUITextField:UITextField{
            NSInteger rowNumber;
        }
    

    在 Utility.m 中

    @implementation CustomUITextField
    
    @synthesize rowNumber;
    @end
    

    我的 tableView cellForRowAtIndexPath 方法是

    - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *Identifier = @"Cell";
        UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:Identifier];
        if(cell == nil)
            cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath];
    
        CustomUITextField *itemNameTextField = (CustomUITextField *)[cell.contentView viewWithTag:TEXTFIELD_TAG];//this is the tag I have set in reuseTableViewCellWithIdentifier method for textfield
        itemNameTextField.rowNumber = indexPath.row;
        itemNameTextField.text = @"";//you can set it for the value you want
        if(itemListTable.editing)
            itemNameTextField.borderStyle = UITextBorderStyleRoundedRect;
        else
            itemNameTextField.borderStyle = UITextBorderStyleNone;
    
    
    
        return cell;
    
    }
    

    您可以为 CustomUITextField 自定义 UITextField 的委托方法,并且可以通过访问 CustomTextField 的行号来保存在特定行的文本字段中输入的文本。

    试试这个。

    【讨论】:

    • 最好使用@property (nonatomic, retain) NSIndexPath *indexPath;
    【解决方案4】:

    我遇到了同样的问题,我发现一些代码可以解决这个问题。它将输入的数据放入数组查看调试器控制台以查看输入文本的结果这里是链接TextFieldCell.。快乐编码

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2011-09-19
      • 1970-01-01
      • 2015-05-27
      • 2019-11-27
      • 2010-09-25
      相关资源
      最近更新 更多