ios - 单击按钮时将表视图数据存储在数组中 - 目标 c
问题描述
我有一个UITableView
每个单元格都有文本字段。我想获取写在这些文本字段中的文本NSMutableArray
目前正在点击按钮执行此操作 -
- (void)viewDidLoad {
[super viewDidLoad];
_optionTextArray = [[NSMutableArray alloc] init];
}
- (IBAction)saveClicked:(UIButton *)sender {
editVotingOptionCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell"];
for (int i=0; i<_optionArray.count; i++) {
[_optionTextArray addObject:cell.tfOption.text];
}
}
但是每次都存储空字符串。
解决方案
@interface MyCell: UITableViewCell {
@property(strong) UITextField *textField;
}
假设您有一个带有类的单元格,MyCell
当您的按钮被点击时,首先获取部分中的行数。让你有一个默认部分,0
这样在循环遍历后获取第 0 部分的行数,并在每个 indexPath 处获取单元格并从该 indexPath 获取文本并检查文本是否为空,最后将其插入到大批。下面是按钮单击的示例代码。
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0]
NSMutableArray *allTexts = [NSMutableArray new];
for (NSInteger i = 0; i< numberOfRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
MyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.textField.text != nil) {
[allText addObject:cell.textField.text];
}
}
推荐阅读
- php - 在 Woocommerce 中用自定义文本标签替换零或空显示价格
- php - Codeigniter 锚标记超链接到控制器中的表单,但数据显示在表外
- reactjs - awsmobile 配置 -> 错误:标头内容包含无效字符
- java - 如何使用 LocalDate - Java 获得一个月中的工作日数量
- python - 为什么我需要在 setup.py 中包含子包
- c# - 使用 SQL 调用从数据库中检索数据后的特定 if else 语句。视觉工作室 2015 / C#
- postgresql - Postgres 简单 tsquery 失败,但应该可以工作
- javascript - 使用回调/匿名函数进行迭代
- wpf - 为什么我的 WPF ListView 列不正确对齐?
- algorithm - 优先队列中的饥饿预防