![](/img/trans.png)
[英]How to access UITableViewCell from UITableView in different UIViewController?
[英]Access properties of a UITableViewCell from the UIViewController
我正在编写一个在UIViewController
内具有UITableView
的应用程序。 UITableView
使用原型单元,每个单元内部都有一个UITextField
。 您可能会猜到,该视图是用户要填写的表单。
当用户单击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);
希望这会对您有所帮助.. :-)
我假设您的AddCardCell
是文本字段的委托。 最好的方法是让您的单元格类声明其自己的委托协议。 然后,您的视图控制器将成为单元的委托。 然后,您的单元格应实现文本字段的委托方法,以检测文本何时更改。 然后,单元格应该告诉其委托人有新文本。 文本应该是委托方法中的参数之一。 您的视图控制器将实现此单元委托方法。 然后,视图控制器将使用更新后的文本来更新数据模型。
轻按按钮后,来自所有单元格的文本将已经在数据模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.