ios - 如何将 UITextField 的值转换为 UITableViewCell 中的 NSString?

标签 ios objective-c uitableview uitextfield nsstring

我将 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/

相关文章:

ios - UIDatePicker 截断文本不显示天数

ios - 在 iOS 中使用 NSNotificationCenter 传递 userInfo 数据的更好方法

ios - 调试 Firebase 崩溃报告 - CALayer 位置包含 NAN

iphone - 如何解决这个内存泄漏?

ios:在点击 collectionview 单元格或 tableview 单元格时禁用第二个触摸事件

objective-c - 检测数组中不同字符串的数量

iphone - UITableView 末尾下方的幻影 "cells"

ios - 将 View Controller 添加到另一个时的自定义转换

ios - 如何使用 CocoaPods 正确安装 ReactiveCocoa?

Objective-C:我们应该在 block 内使用 weak self 还是在使用它之前将 weak self 分配给 strong ?