【问题标题】:Fetching Values from textField in a custom cell iPhone从自定义单元格 iPhone 中的 textField 获取值
【发布时间】:2011-09-08 06:55:50
【问题描述】:

我在表格视图中有一个自定义单元格,见下文。

我的自定义单元格中有两个文本字段 txt1 和 txt2,如下所示

如何访问我在每个文本字段中输入的值,以便将值添加到单独的数组中...

“Addset”按钮将通过增加一个集合来增加分组表视图的部分数。

我的表格视图代码如下。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *cellID= @"catogoryCell";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell==nil)
    {
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass: [CustomCell class]])
            {
                cell = (CustomCell *)currentObject;
            }
        }
    }

    //cell.txt1.text = @"0"; 

    return cell;
}

谢谢大家..

【问题讨论】:

    标签: iphone ios uitextfield


    【解决方案1】:

    单元格是可重复使用的,因此它们需要一种更持久的方式来保存其信息。

    方法一: 您可以将一些 UITextField 对象保存到一个数组中,以防您也不想重用文本字段,并且在 cellForRowAtIndexPath 您只需将文本字段设置为它们的单元格,例如:

    cell.txt1 = [textFieldsArray objectAtindex:indexPath.section*2];
    cell.txt2 = [textFieldsArray objectAtindex:indexPath.section*2+1]; //txt1 and txt2 properties should have assign
    

    方法二: 如果您还想重用文本字段,我建议使用带有可变字典的数组,每个字典都保存一个单元格的“设置”。文本字段将完全由自定义单元格管理(例如:在UIControlEventValueChanged 事件中更新附加到单元格的字典中的@"txt1" 或@"txt2" 值)。

    ///somewhere in the initialization (in the class holding the tableview)
    contentArray = [[NSMutableArray alloc] init];
    
    ///when adding a new cell (e.g: inside the 'Add set' action)
    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"", @"txt1", @"", @"txt2", nil]];
    //add a new cell to the table (the same way you do now when tapping 'Add set')
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        ...
         [cell attachDictionary:[contentArray objectAtIndex:indexPath.section]];
        return cell;
    }
    
    ///anywhere where you'd like to access the values inserted inside a cell
    NSMutableDictionary *cell3Content = [contentArray objectAtIndex:3];
    NSString *text1 = [cell3Content valueForKey:@"txt1"];
    NSString *text2 = [cell3Content valueForKey:@"txt2"];
    
    ///CustomCell.m
    -(id)initWithCoder:(NSCoder *)decoder{
        self = [super initWithCoder:decoder];
        if(!self) return nil;
        [txt1 addTarget:self action:@selector(txt1Changed:) forControlEvents:UIControlEventValueChanged];
        [txt2 addTarget:self action:@selector(txt2Changed:) forControlEvents:UIControlEventValueChanged];
        return self;
    }
    -(void)attachDictionary:(NSMutableDictionary *)dic{
        contentDictionary = dic;
        txt1.text = [contentDictionary valueForKey:@"txt1"];
        txt2.text = [contentDictionary valueForKey:@"txt2"];
    }
    -(void)txt1Changed:(UITextField *)sender{
        [contentDictionary setValue:txt1.text forKey:@"txt1"];
    }
    

    【讨论】:

    • HI 谢谢babbiidi,textFieldarray 中的内容是什么??
    • textFieldsArray 将保存UITextField 对象,可以在点击“添加集”时对其进行初始化和设置。它们可以正确定位在单元格的 txt1txt2 设置器内的自定义单元格中。我个人更喜欢第二种方法,因为每个单元格都会处理它的内容,而且它会使用更少的内存。
    • 嗨,巴比迪,我试过你的代码,但哪里出了问题。视图加载时应用程序崩溃。你能提供更多的东西吗? contentArray 是否包含 textField 对象??
    • 否,对于第二种方法,contentArray 包含 NSMutableDictionary,在键上有 2 个值:@"txt1"@"txt2"(这将是插入到单元格文本字段中的文本)。此外,请确保在CustomCell.xib 中,您有 txt1 和 txt2 链接到来自CustomCell 类的IBOutlet UITextfield *txt1;IBOutlet UITextField *txt2(不是所有者,而是单元格本身 - 在 xib 中使其类型为CustomCell)。上面第二个块的代码(主要注释)是关于你需要的一切。
    【解决方案2】:

    当您在 UITableViewCell 子类中建立 IBOutlet 连接时,将它们连接到文件所有者(viewController)中的属性,而不是视图本身。这样你就可以从你的 viewController (UItableViewDataSource) 访问它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      相关资源
      最近更新 更多