【问题标题】:Access textfield.text for textfield inside a cell of tableView访问 tableView 单元格内 textfield 的 textfield.text
【发布时间】:2016-05-21 22:37:55
【问题描述】:

我正在尝试提取输入到文本字段的值。问题是这些文本字段是在表格视图的单元格中以编程方式定义的。

-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:(BOOL)animated];
NSUInteger numOfRows=[userControls count];
for (int i =0; i<numOfRows; i++) {
    [self saveValueForRow:i];
}

}

#pragma save defaults
- (void)saveValueForRow :(int)index {
    NSString *userDataString=userEntryField.text; //how to access each text field, as this currently just accesses the first one in the table
    NSLog(userDataString);
   [[NSUserDefaults standardUserDefaults] setObject:userDataString   forKey:[controlKeys objectAtIndex:index]];
}

我的直觉是在视图消失后收集所有数据。但我不知道如何访问每个不同单元格中的每个不同文本字段。

【问题讨论】:

  • 在单元格文本字段中输入文本后,用户应该点击任何按钮来保存数据或任何其他选项......

标签: ios objective-c uitableview uitextfield


【解决方案1】:

需要在cellForRowAtIndexPath方法中设置cell的标签为:

cell.tag = indexPath.row;

现在在获取 textField 的值时,您需要首先通过 indexPath 获取单元格,例如:

#pragma save defaults
- (void)saveValueForRow :(int)index {
NSIndexPath *nowIndex = [NSIndexPath indexPathForRow:index inSection:0]
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];

NSString *userDataString = cell.userEntryField.text; //how to access each text field, as this currently just accesses the first one in the table
NSLog(userDataString);
[[NSUserDefaults standardUserDefaults] setObject:userDataString   forKey:[controlKeys objectAtIndex:index]];
}

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    您可以为每个UITextField 设置一个tag,例如:

    //...
    textview.tag = indexPath.row;
    //...
    

    因此,您可以通过tag 访问每个textField。我希望这可以帮助你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多