【问题标题】:How to get the textfield value of each dynamically created cell in UITableView如何获取 UITableView 中每个动态创建的单元格的文本字段值
【发布时间】:2016-01-07 08:53:10
【问题描述】:

我有一个表格视图,其中根据遇到的数据类型为许多行创建/重用自定义单元格(即对于 Date 类型,单元格的文本字段从 datePicker 中选择选定的日期,对于 enum 类型,它选择选定的来自下拉选择器的数据)。现在页脚有一个按钮,需要从每个单元格中提取数据并对其进行处理以显示更多视图控制器。

由于单元格中的数据在选择后(从日期选择器或下拉菜单或键盘)显示良好,但我们如何在页脚按钮方法中捕获这些数据。

这里是代码:-

- (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 ?


}

请帮忙。

【问题讨论】:

  • 我真的建议为此创建自定义单元格类,这样您就可以为其创建一个委托或传递代码块,否则您必须创建标签(不推荐)或使用@987654322 将点转换为单元格@
  • 有两个选项:TAG 和 DELEGATE。

标签: ios objective-c uitableview


【解决方案1】:

您的代码似乎有问题:

[cell addSubview:scheduelText];
[cell addSubview:dateText];
[cell addSubview:accountText];

每次使单元格出列时,都会向单元格添加一个新的子视图,但不会被删除。 在我的建议中,你最好用不同的标识符识别 4 种单元格,或者在单元格出列时删除子视图。

【讨论】:

  • 是的,莱昂,你是对的。它只是覆盖了以前的单元格,并且值正在丢失。
【解决方案2】:

您可以为每个文本字段设置标签。并从标签中获取 UITextField。

 - (void)submitButtonPressed:(id)sender
{

        UITableViewCell *cell =[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

        UITextField *txtField =[cell viewWithTag:TAG];

        NSLog(@"%@",txtField.text);
}

另外,请为此使用自定义单元格。

【讨论】:

    【解决方案3】:

    由于您使用的是动态表格视图,因此只有一个选项。委派您的控件并将其值存储在“endEditing”上。您不能使用 viewTag,因为如果在屏幕上不可见,视图将被破坏。 另一种选择是使用静态表格视图,然后您可以为每个控件创建@IBOutlets。

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      相关资源
      最近更新 更多