Ios 如何在UITableView中获取每个动态创建的单元格的textfield值
我有一个tableview,其中根据遇到的数据类型为许多行创建/重用自定义单元格(即,对于日期类型,单元格的textfield从datePicker中拾取所选日期,对于enum类型,它从下拉选择器中拾取所选数据)。现在,在页脚有一个按钮,它需要从每个单元格中提取数据并对其进行处理,以显示进一步的视图控制器 由于单元格中的数据在选择后(从日期选择器、下拉菜单或键盘)显示良好,但我们如何在页脚按钮方法中捕获这些数据 以下是代码:-Ios 如何在UITableView中获取每个动态创建的单元格的textfield值,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个tableview,其中根据遇到的数据类型为许多行创建/重用自定义单元格(即,对于日期类型,单元格的textfield从datePicker中拾取所选日期,对于enum类型,它从下拉选择器中拾取所选数据)。现在,在页脚有一个按钮,它需要从每个单元格中提取数据并对其进行处理,以显示进一步的视图控制器 由于单元格中的数据在选择后(从日期选择器、下拉菜单或键盘)显示良好,但我们如何在页脚按钮方法中捕获这些数据 以下是代码:- - (NSInteger)tableView:(UITableVie
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [valueArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *values = [valueArray objectAtIndex:indexPath.row];
NSString *titles = [titleArray objectAtIndex:indexPath.row];
if([titles isEqualToString:@“enumType”]){
cell.headerInfo.hidden = true;
cell.valueObtained.hidden=true;
UITextField * scheduelText = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 130, 30)];
NSMutableArray* enumArray = [[NSMutableArray alloc] init];
[enumArray addObject:@"BUSINESS"];
[enumArray addObject:@"CUSTODIAL"];
[enumArray addObject:@"INDIVIDUAL"];
downPicker = [[DownPicker alloc] initWithTextField:scheduelText enumArray];
scheduelText.text = downPicker.text;
[cell addSubview:scheduelText];
}
else if([titles isEqualToString:@"DateType”]){
cell.headerInfo.hidden = true;
cell.valueObtained.hidden=true;
UITextField *dateText = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 130, 30)];
dateText.placeholder=@"Date";
dateText.inputView = self.datePicker;
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *doItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dateDoneButton)];
[toolBar setItems:@[doItem]];
dateText.inputAccessoryView = toolBar;
[cell addSubview:dateText];
}
else if([titles isEqualToString:@“TextType”]){
cell.headerInfo.hidden = true;
cell.valueObtained.hidden=true;
UITextField * accountText = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, 130, 30)];
accountText.returnKeyType = UIReturnKeyDone;
accountText.delegate = self;
accountText.placeholder=@"Account Name";
[accountText setTextColor:[UIColor blackColor]];
[accountText setBorderStyle:UITextBorderStyleNone];
[cell addSubview:accountText];
}
else{
cell.valueObtained.text = values;
cell.headerInfo.text = titles;
}
return cell;
}
- (void)submitButtonPressed:(id)sender
{
//Need to capture the cell data here ?
}
请在这方面提供帮助。您可以为每个文本字段设置标记。并从标记中获取UITextField
- (void)submitButtonPressed:(id)sender
{
UITableViewCell *cell =[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UITextField *txtField =[cell viewWithTag:TAG];
NSLog(@"%@",txtField.text);
}
另外,请使用自定义单元格进行此操作。您的代码似乎有问题:
[cell addSubview:scheduelText];
[cell addSubview:dateText];
[cell addSubview:accountText];
每次退出队列时,单元都会向单元添加新的子视图,但不会删除。
在我的建议中,您最好使用不同的标识符标识4种单元格,或者在单元格出列时删除子视图。因为您使用的是动态表视图,所以只有一个选项。委派控件并将其值存储在“endEditing”上。您不能使用viewTag,因为如果视图在屏幕上不可见,视图将被破坏。
另一个选项是使用静态tableview,然后您可以为每个控件创建@IBOutlets。我确实建议为此创建自定义单元格类,这样您就可以为其创建委托或传递代码块,否则您必须创建标记(不推荐)或者使用
indexPathForRowAtPoint
将点转换为单元格有两个选项:标记和委托。是的,Leon,你是对的。它只是覆盖上一个单元格,而值正在丢失。