UITextField在UITableViewCell帮助中

时间:2010-12-31 05:11:08

标签: iphone ipad uitableview ios uitextfield

我已经在互联网上搜索了一个很好的教程,或者在每个单元格中都有一个UITextField填充UITableView以进行数据输入。

我希望在滚动时跟踪每个UITextField及其中写入的文本。 tableView将被分区。我一直在使用自定义的UITableViewCell,但我对任何方法都持开放态度。

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

如果你们中的任何人能指出我正确的方向,我们将不胜感激。

提前谢谢!

4 个答案:

答案 0 :(得分:10)

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

创建该数组时,需要将空NSString对象添加到该数组。每次加载单元格时,都必须将受尊重的对象替换为受尊重的textField。

检查以下代码。

- (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];
}

此致

萨蒂亚

答案 1 :(得分:9)

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

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

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

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

答案 2 :(得分:3)

我在tableview中使用了Textfields来输入数据。 我已经在一个名为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;

}

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

试试吧。

答案 3 :(得分:0)

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