我正在尝试提取输入到文本字段的值。问题是这些文本字段是在表格 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/