如何从动态单元ios读取数据
如何获取动态tableview单元格数据。 我有这样一个表视图。所有单元都在动态创建。 我想在按下提交按钮时获得所有文本(味道和%)数据。如何从动态单元ios读取数据,ios,objective-c,uitableview,dynamic,Ios,Objective C,Uitableview,Dynamic,如何获取动态tableview单元格数据。 我有这样一个表视图。所有单元都在动态创建。 我想在按下提交按钮时获得所有文本(味道和%)数据。 问题是我在中动态创建文本。因此,我无法单独识别文本框。如何从动态文本框中获取数据?您可以将标记设置为文本字段,并通过该标记访问它们 比如说, UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 30)]; textField.tag = 100
问题是我在中动态创建文本。因此,我无法单独识别文本框。如何从动态文本框中获取数据?您可以将
标记设置为文本字段
,并通过该标记访问它们
比如说,
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
textField.tag = 100; //set tag
UITextField *textField1 = (UITextField*)[self.view viewWithTag:100]; //retreive by tag
这样,您就可以管理动态生成的textfields
更新:
self.yourTextfield.tag = 100;
self.yourbutton.tag = 100;
// set action method to uibutton
[cell.yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
// here get all ui component using tag
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
然后分别设置两个标签100和101。现在您必须首先获取单元格,然后从该单元格中获取textfields
,然后单击按钮首先获取所有单元格,然后从该单元格中获取textfield
,如下所示:
UITextField *textField = (UITextField*)[cell viewWithTag:100];
因此,您必须在单击按钮时首先获取单元格。您可以将标记设置为文本字段
,并通过该标记访问它们
比如说,
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
textField.tag = 100; //set tag
UITextField *textField1 = (UITextField*)[self.view viewWithTag:100]; //retreive by tag
这样,您就可以管理动态生成的textfields
更新:
self.yourTextfield.tag = 100;
self.yourbutton.tag = 100;
// set action method to uibutton
[cell.yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
// here get all ui component using tag
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
然后分别设置两个标签100和101。现在您必须首先获取单元格,然后从该单元格中获取textfields
,然后单击按钮首先获取所有单元格,然后从该单元格中获取textfield
,如下所示:
UITextField *textField = (UITextField*)[cell viewWithTag:100];
因此,您必须在单击按钮时首先获取单元格。在自定义单元格中将标记设置为uitextfield
For eg.
cell.lblFlavour.tag = indexPath.row;
cell.lblPercent.tag = indexPath.row;
现在,根据自定义单元格中uitextfield的indexPath设置标记访问uitextfield
For eg.
cell.lblFlavour.tag = indexPath.row;
cell.lblPercent.tag = indexPath.row;
现在,根据indexPath访问uitextfield,使用tag
属性唯一地标识每个UI
组件。将创建文本字段和按钮的相同标记
属性指定给每一行,然后在更改标记
后,为下一行指定值,并使用代码获取它们
设置标签
:
self.yourTextfield.tag = 100;
self.yourbutton.tag = 100;
// set action method to uibutton
[cell.yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
// here get all ui component using tag
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
使用tag
属性获取UI
组件:
self.yourTextfield.tag = 100;
self.yourbutton.tag = 100;
// set action method to uibutton
[cell.yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
// here get all ui component using tag
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
或者您可以使用方法didSelectRowAtIndexPath
获取值,请参见代码:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"getting index path %@",indexPath);
NSLog(@"getting index path %@",cell.yourTextField.text);
}
使用tag
属性来唯一地标识每个UI
组件。将创建文本字段和按钮的相同标记
属性指定给每一行,然后在更改标记
后,为下一行指定值,并使用代码获取它们
设置标签
:
self.yourTextfield.tag = 100;
self.yourbutton.tag = 100;
// set action method to uibutton
[cell.yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
// here get all ui component using tag
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
使用tag
属性获取UI
组件:
self.yourTextfield.tag = 100;
self.yourbutton.tag = 100;
// set action method to uibutton
[cell.yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
// here get all ui component using tag
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
或者您可以使用方法didSelectRowAtIndexPath
获取值,请参见代码:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"getting index path %@",indexPath);
NSLog(@"getting index path %@",cell.yourTextField.text);
}
UITableView
的主要思想是将数据存储到数据源中,而不是存储到单元格中。当您点击导航栏上的“+”按钮时,您应该将新项目添加到数组中,然后调用[tableView reloadData]
。因此,UITableView
只表示数组中的数据。当按下submit按钮时,您只需枚举一个数据源数组,并从每行获取所有文本
将文本点击单元格内的文本字段时,可以使用委托
或块
将数据存储到数组中。UITableView的主要思想是将数据存储到数据源,而不是存储到单元格。当您点击导航栏上的“+”按钮时,您应该将新项目添加到数组中,然后调用[tableView reloadData]
。因此,UITableView
只表示数组中的数据。当按下submit按钮时,您只需枚举一个数据源数组,并从每行获取所有文本
将文本点击单元格内的文本字段时,可以使用委托
或块
将数据存储到数组中。尝试以下代码:
当你点击提交按钮时
for (int i=0; i < [[tableView visibleCells] count]; i++)
{
//yourArrayname is the number of rows in the UITableView /Or UITableView visible cells
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0];
CustomTableViewCell *cell = [tblExperience cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UITextField class]]){
UITextField* CurrentTextField = (UITextField *)view;
NSLog(@"val %d %@",i,CurrentTextField.text);
[finalArray addObject:CurrentTextField.text];
}
}
}
for(int i=0;i<[[tableView VisibleCell]计数];i++)
{
//yourArrayname是UITableView/或UITableView可见单元格中的行数
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:i第1节:0];
CustomTableViewCell*单元格=[tblExperience CellForRowatineXpath:indexPath];
for(UIView*cell.contentView.subview中的视图){
if([view iskindof类:[UITextField类]]){
UITextField*CurrentTextField=(UITextField*)视图;
NSLog(@“val%d%@”,i,CurrentTextField.text);
[finalArray addObject:CurrentTextField.text];
}
}
}
尝试以下代码:
当你点击提交按钮时
for (int i=0; i < [[tableView visibleCells] count]; i++)
{
//yourArrayname is the number of rows in the UITableView /Or UITableView visible cells
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0];
CustomTableViewCell *cell = [tblExperience cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UITextField class]]){
UITextField* CurrentTextField = (UITextField *)view;
NSLog(@"val %d %@",i,CurrentTextField.text);
[finalArray addObject:CurrentTextField.text];
}
}
}
for(int i=0;i<[[tableView VisibleCell]计数];i++)
{
//yourArrayname是UITableView/或UITableView可见单元格中的行数
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:i第1节:0];
CustomTableViewCell*单元格=[tblExperience CellForRowatineXpath:indexPath];
for(UIView*cell.contentView.subview中的视图){
if([view iskindof类:[UITextField类]]){
UITextField*CurrentTextField=(UITextField*)视图;
NSLog(@“val%d%@”,i,CurrentTextField.text);
[finalArray addObject:CurrentTextField.text];
}
}
}
为文本字段分配动态标记,实现文本字段委托,并在文本字段结束编辑时将数据保存在数组中。像这样的
-(void)textfielddidediting:(UITextField*)textField{
[_arrayinsertobject:textField.text atIndex:textField.tag];
}
为文本字段分配动态标记,实现文本字段委托,并在文本字段结束编辑时将数据保存在数组中。像这样的
-(void)textfieldDendediting:(UITextField*)textField