ios - 访问 tableView 单元格内文本字段的 textfield.text

标签 ios objective-c uitableview uitextfield

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

-(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]];
}

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

最佳答案

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

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]];
}

希望对您有所帮助!

关于ios - 访问 tableView 单元格内文本字段的 textfield.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37368680/

相关文章:

ios - 加载 TableView 时关闭 UIAlertController

iphone - UIPinchGestureRecognizer 中捏合缩放的最大/最小比例 - iPhone iOS

ios - 将日期格式化为固定宽度且不补零

objective-c - Cocos2d调度方法中的选择器

ios - 亚马逊 Cognito iOS

ios - 从数组设置自定义UITableViewCell的文本

iphone - iOS CXMLElement 字符串值

ios - 注册。使用Delphi XE7开发iOs移动应用程序的硬件要求

ios - 从 performSelector 传递对象

ios - 使用 UITableView 时应用程序崩溃