【问题标题】:How to get all values from all textfields如何从所有文本字段中获取所有值
【发布时间】:2015-06-30 15:42:25
【问题描述】:

在谷歌搜索这个问题后,我设法做到了这一点

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSString *cellIdentifier = @"InsertMarksCell";
    SaveAllExamMarksForAllStudentsTableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    StudentPersonalInfo *newStudentName = feedItemsNow[indexPath.row];

    myCell.txtStudentMark.text = @"hello";
    myCell.txtStudentMark.delegate = self;
    myCell.txtStudentMark.tag = indexPath.row;

    return myCell;
}

此代码我设置文本字段“txtStudentMark”委托并设置它的标签...

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    NSString *text = [(UITextField *)[self.view viewWithTag:55] text];
    NSLog(@"%@",text);
}

我使用 NSLog 函数不断得到空值

我在自定义单元格中有一个文本字段,用户将填充数据,我需要从所有文本字段中获取所有数据,我走对了吗?

据我了解,我需要为 textField 设置标签并将其设置为委托,然后我可以通过标签调用 textfield 以获取其中的文本。

我怎样才能完成这项工作?

【问题讨论】:

  • 如果textnil,那么这意味着self.viewnil,或者self.view 没有标签为55 的子视图。
  • 我有一个标签 55 ...我已经用 "hello" 填充了文本字段,只是编辑了代码并添加了它,所有文本字段都有 "hello" 文本。文本字段在@interface SaveAllExamMarksForAllStudentsTableViewCell 中声明: UITableViewCell 我如何调用文本字段?
  • textfield 是 tableViewCell 的子视图?对吗?

标签: ios objective-c uitextfield tableviewcell


【解决方案1】:

你为什么不使用

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    NSString *text = [textField text];
    NSLog(@"%@",text);
} 

您的问题是您将文本字段添加到单元格中,但要求查看此标签。

编辑:

从 tableview 获取您的文本字段

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    UITableViewCell *cell = [self.view.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:55 inSection:0]];

    NSString *text = [(UITextField *)[cell.contentView viewWithTag:55] text];
    NSLog(@"%@",text);
}

【讨论】:

  • 很好,一旦完成编辑,就可以工作并打印所有值......但是,我可以通过 TAG 调用文本字段吗?
  • UITableViewCell *cell = [self.view.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:55 inSection:0]];在“self.view.tableView”我无法将其设置为我的 tableView self.view.StudentsNameIDMarksTableView 错误:在对象类型 UIView 上找不到属性 StudentsNameIDMarksTableView
  • @jack,只能通过您的表格视图,因为您的文本字段位于单元格的内容视图中。请参阅我的编辑。视图层次结构看起来像 uitableview - uitableviewcell - contentview - your_textfield。我可能会错过一些时间视图,但顺序和我提到的一样
  • @jack,使用自我。学生姓名IDMarksTableView
  • 使用自我。 StudentsNameIDMarksTableView ,我得到 Null 值,我也将标签更改为 0 而不是 55,因为“ indexPath.row 必须从 0 开始,所以我知道 tag0 将存在,但仍然是空值 ...
【解决方案2】:

@Doro 感谢您的帮助。

既然问题解决了,这就是对我来说效果很好的答案

通过TAG获取textfield中的行号和数据

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    // here get the tag number which is the row number
    NSInteger *rowNumberWithout200 = (long)textField.tag;
    NSString  *myRow = [NSString stringWithFormat: @"%ld", rowNumberWithout200];
    NSLog(@"row %@",myRow);

    // get the text in the cell with the required tag...
    UITableViewCell* myCell = (UITableViewCell*)textField.superview;
    NSString *StudentMark = [(UITextField *)[myCell viewWithTag:textField.tag] text];
    NSLog(@"mark %@",StudentMark);
}

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2016-03-31
    相关资源
    最近更新 更多