简体   繁体   English

iOS从tableViewCell中的文本字段获取文本值

[英]ios get text value from textfield in tableViewCell

In my ViewController, I had created a textField with name UITextField *txtMyName; 在ViewController中,我创建了一个名称为UITextField *txtMyName;的textField UITextField *txtMyName; After that, I was added my textField to tableViewCell [cell addSubview:txtMyName]; 之后,我将我的textField添加到tableViewCell [cell addSubview:txtMyName];

I can edit text in my textField, but when I get text from textField _strFullName = txtMyName.text; 我可以在textField中编辑文本,但是当我从textField中获取文本时_strFullName = txtMyName.text;

But, it's get empty values. 但是,它得到的是空值。

How to I can resolve this problem! 我该如何解决这个问题!

***** Edited**** *****编辑****

if (indexPath.row == 0) {
        [self setFrameTextField:cell];
        [self CustomTextField:cell :txtMyName];
        if ([[dict_infoFriend objectForKey:@"Fullname"] isEqualToString:@" "]) {
            txtMyName.placeholder = @"Name";
        }
        else{
            txtMyName.text = [dict_infoFriend objectForKey:@"Fullname"];
        }
        [cell addSubview:txtMyName];
    }
else if (indexPath.row == 1){
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            button.frame = CGRectMake(cell.frame.size.width - 10, 0, 130, 40);
        }
        else{
            button.frame = CGRectMake(cell.frame.size.width / 2 - 65, 0, 130, 40);
        }
        [self CustomBTN:button];
        [button addTarget:self action:@selector(ClickDone) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];
    }

you haven't assign memory to your textfield yet thats why you are able to edit but not able to retrive it. 您尚未为文本字段分配内存,这就是为什么您能够编辑但无法检索它的原因。 just write txtMyName = [[UITextfield alloc]init]; 只需写txtMyName = [[UITextfield alloc]init]; you can assign frame to textfield like txtMyName = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,width,height)]; 您可以将框架分配给txtMyName = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,width,height)];txtMyName = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,width,height)]; once you assign memory to your textfield then you will be retrive the text from textfield 将内存分配给文本字段后,您将从文本字段中检索文本

Since you have added the textfield into table cell, you've to get the cell first then using tag of textfield you can get the text. 由于您已将文本字段添加到表格单元格中,因此必须首先获取该单元格,然后使用文本字段的标记才能获取文本。

Try like this, 这样尝试

Assume that you have tag for textfield like txtMyName.tag = 100 假设您有文本字段的标签,例如txtMyName.tag = 100

-(void) ClickDone {
      NSIndexPath *indexPath = [[NSIndexPath alloc] indexPathForRow:0 inSection:0];
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
     UITextField *txtField = (UITextField *)[cell viewWithTag:100];
     _strFullName = txtField.text;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM