【问题标题】:Get textfields values in dynamic tableview在动态表格视图中获取文本字段值
【发布时间】:2012-08-09 11:30:35
【问题描述】:

我有一个动态表格视图,它有 5 个原型单元格,在每个单元格内我有 6 个文本字段。我正在标记文本字段,但我无法理解 如何从“textFieldDidEndEditing”中的所有文本字段中获取值。 在我的代码中,我有这个:

-(void) textFieldDidEndEditing:(UITextField *)textField
{
NSMutableArray *cellOneContentSave = [[NSMutableArray alloc] init];
NSString *cellOneTexfieldOneTxt;
if (textField == [self.view viewWithTag:1503])
{
cellOneTexfield1Txt = textField.text;
[cellOneContentSave addObject:cellOneTexfieldOneTxt];  
}

问题 1: 但是!这只能让我从单元格一中的一个 texfield 获得值...我应该为每个单元格和 texfield 使用一个开关吗?。

问题2:我说的是动态表格视图,所以用户可以在进入提交编辑样式时按下左侧出现的绿色+按钮插入新闻行(每节) ...当他进入时,newtexfields 的标签是否应该有不同的标签?一方面我认为不是,因为它是新的 texfields 但不同的 indepaxth.row ......但另一方面我不知道控制器是否需要新标签......

【问题讨论】:

    标签: uitableview tags uitextfield nsindexpath


    【解决方案1】:
    -(void) textFieldDidEndEditing:(UITextField *)textField
    {
        // assuming your text field is embedded directly into the table view
        // cell and not into any other subview of the table cell
        UITableViewCell * parentView = (UITableViewCell *)[textField superview];
    
        if(parentView)
        {
            NSMutableArray *cellOneContentSave = [[NSMutableArray alloc] init];
            NSString *cellOneTexfieldOneTxt;
    
            NSArray * allSubviews = [parentView subviews];
            for(UIView * oneSubview in allSubviews)
            {
                // get only the text fields
                if([oneSubview isKindOfClass: [UITextField class]])
                {
                    UITextField * oneTextField = (UITextField *) oneSubview;
    
                    if(oneTextField.text)
                    {
                        [cellOneContentSave addObject: oneTextField.text];
                    } else {
                        // if nothing is in the text field, should
                        // we simply add the empty string to the array?
                        [cellOneContentSave addObject: @""];
                    }
                }
            }
        }
    
        // don't forget to actually *DO* something with your mutable array
        // (and release it, in case you're not using ARC), before this method
        // returns.
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2011-07-29
      相关资源
      最近更新 更多