我将 UITextField
设置为 UITableViewCell
。根据数组计数加载 UITableView
。所有文本字段都是可编辑的。但是,我希望当我编辑 UITextField
时,UITextField
数据应该存储到预定义的 NSString
中。对于每个 IndexPath.row 的文本字段都是不同的字符串。
附上我用来生成 NSMutableArray
的代码...
[arrProfile removeAllObjects];
arrProfile = [[NSMutableArray alloc] initWithObjects:
@{@"title": @"CUSTOMER NAME", @"details": strFName},
@{@"title": @"EMAIL ID", @"details": strEmail},
@{@"title": @"MOBILE NO.", @"details": strContact},
@{@"title": @"STATE", @"details": strStateName},
@{@"title": @"CITY", @"details": @""},
nil];
[_tblProfile reloadData];
现在附加在 cellForRowAtIndexPath 中使用的代码...
static NSString *cellIdentifier = @"cellProfile";
NSDictionary *dictProfile = [arrProfile objectAtIndex:indexPath.row];
ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.lblTitle.text = [dictProfile objectForKey:@"title"];
cell.txtDetails.text = [dictProfile objectForKey:@"details"];
cell.txtDetails.delegate = self;
[self textFieldShouldReturn:cell.txtDetails];
[self textFieldShouldBeginEditing:cell.txtDetails];
return cell;
最佳答案
如果我正确理解您的问题,您想要构建一个动态表单,其中许多文本字段会根据您提供的数组而变化。您正在尝试通过采用 TableView 并按每行加载所有文本字段来实现此目的。现在的问题是如何获取每个文本字段中输入的值。如果这是你的问题,这里是如何去做的事情。
您可以使用文本字段标签来做到这一点。您没有听错!为此,在 tableview 单元格方法中为每个文本字段提供唯一标记(您可以使用 indexpath.row)。在这里分配代表。将触发文本字段委托(delegate)方法。现在,再次基于标签的文本字段委托(delegate)方法从文本字段获取输入作为字符串并存储。
注意:
[self textFieldShouldReturn:cell.txtDetails];
[self textFieldShouldBeginEditing:cell.txtDetails];
不需要从 tableview cellForRowAtIndexPath 调用的方法。如果您分配文本字段委托(delegate),它将处理它。
关于ios - 如何将 UITextField 的值转换为 UITableViewCell 中的 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50056527/