【问题标题】:Getting data from a TextField in a cell从单元格中的 TextField 获取数据
【发布时间】:2013-02-09 19:20:30
【问题描述】:

我创建了一个带有 TabelViewCell 的笔尖,其中包含一个标签和一个 TextField,然后我将此单元格加载到只有两行的 TableView 中。我想捕获一个项目的名称和价格,所以我更新了 cellForRowAtIndexPath 方法中的每个标签,因为我可以访问 indexPath.row 值。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleCellIdentifier = @"AddBudgetItemCell";

    SCAddBudgetItemCell *cell = (SCAddBudgetItemCell *)[tableView dequeueReusableCellWithIdentifier:simpleCellIdentifier];

    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleCellIdentifier owner:self options:nil];

        cell = [nib objectAtIndex:0];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataTextField.delegate = self;

    switch (indexPath.row)
    {
        case 0:
            cell.titleLabel.text = @"Item";
            break;

        case 1:
            cell.titleLabel.text = @"Price ¥";
            break;

        default:
            break;
    }

    return cell;
}

但是我不知道如何从 TextField 中检索数据,因为我无法访问我创建的变量“cell”和 IndexPath.row 值。

【问题讨论】:

    标签: ios uitableview textfield


    【解决方案1】:

    据我了解,您有两种选择。如果在您的SCAddBudgetItemCell 内部,您有一个包含文本字段字符串的属性,那么您可能可以通过执行cell.yourtextfield.text 之类的操作从视图控制器访问它(其中您的文本字段是 SCAddBudgetItemCell 中的 UITextView。

    另一种选择是让视图控制器响应 UITextFieldDelegate 并在用户通过实现委托方法对其进行编辑时存储字符串

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        //Whenever people start editing your textfield
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textField{
         //
    return YES;
    }
    -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        //This is obvious. Whenever they end
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2011-05-11
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多