我正在编写一个在 UIViewController
中有一个 UITableView
的应用程序。 UITableView
使用原型(prototype)单元格,每个单元格内部是一个 UITextField
。正如您可能猜到的那样, View 是用户要填写的表单。
当用户单击 UI 中的按钮时,应触发一个事件,其中收集正在显示的每个 UITextField
的所有文本值,以便可以将数据发送到服务器。
我在访问单元格的 UITextField
属性时遇到问题,因此我无法获取文本值。
我的部分代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIDentifier = @"Cell";
AddCardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIDentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[AddCardCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIDentifier];
}
cell.campoTextField.placeholder = [placeholders objectAtIndex:indexPath.row];
cell.tituloLabel.text = [titulosCampos objectAtIndex:indexPath.row];
return cell;
}
我的数据源的设置:
titulosCampos = [NSArray arrayWithObjects: @"Nombre", @"Correo", nil];
placeholders = [NSArray arrayWithObjects: @"Oscar Swanros", @"dev@swanros.com", nil];
最佳答案
您可以检索存储在所有单元格 campoTextField
中的文本,然后使用此代码......
NSMutableArray *titulosCampoTextArray = [[NSMutableArray alloc] init];
for (int i = 0; i < titulosCampos.count; i++) {
AddCardCell *cell = (AddCardCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[titulosCampoTextArray addObject:cell.campoTextField.text];
}
NSLog(@"Data in titulosCampoText array %@",titulosCampoTextArray);
希望这对你有帮助..:-)
关于ios - 从 UIViewController 访问 UITableViewCell 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696756/