【问题标题】:How to get dynamically created UITextField (on top of UITableViewCell ) text value using tag?如何使用标签获取动态创建的 UITextField(在 UITableViewCell 之上)文本值?
【发布时间】:2015-12-06 06:54:47
【问题描述】:

我有一个 UITableView,并且在那个 UITableViewCell 中动态创建了“n”个 UITextField。如何使用标签获取我在 UITextField 中输入的值?

这就是我动态创建 UITextField 的方式

-(UITextField *)displayTextfield:(NSUInteger)index{


    textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
    textField.placeholder=@"Text here";
    textField.tag=index;
    textField.delegate=self;

    textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    textField.textAlignment=UITextAlignmentLeft;
    textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
    textField.textColor=[UIColor darkGrayColor];
    textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    return textField;
}

这就是我尝试访问 UITextField 值但它崩溃的方式。

 NSString *text = [(UITextField *)[cell.contentView viewWithTag:index] text];

index(标签)是否从 for 循环中获取。

-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView text]: unrecognized selector sent to instance 0x7fa8329d9360'

请帮我解决这个问题

displayTextfield() 从这里调用

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier;
    if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
        MyIdentifier = @"CharCell";

    }else{
        MyIdentifier = @"IntCell";
    }

    cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:MyIdentifier];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;               

        [cell.contentView addSubview:[self displayTextfield:indexPath.row]];

    }

return cell;
}

【问题讨论】:

  • 你在哪里调用displayTextfield函数?你也可以给我们看看那部分吗?
  • @ozgur 更新问题

标签: ios objective-c uitableview uitextfield


【解决方案1】:

cell.contentView.subviews 中的零索引已被名为 UITableViewCellContentView 的特殊视图占用,因此当 indexPath.row0 时,以下行不会返回 UITextField:

>>> NSLog("%@", [cell.contentView viewWithTag:0])
<UITableViewCellContentView: 0x7fb7c2eb1cd0; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x7fb7c2eb22a0>; layer = <CALayer: 0x7fb7c2eb1e40>

将您的标签值更改为更大的值,这应该可以解决问题:

[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];

所以当你需要再次获取 UITextField 时:

NSString *text = [(UITextField *)[cell.contentView viewWithTag:indexPath.row + 1] text];

【讨论】:

  • 让我自己试试
【解决方案2】:

将函数的第一行修改为:

UITextField * textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];

其次,将标签 +1 分配给文本字段,使其不为零:

[cell.contentView addSubview:[self displayTextfield:indexPath.row + 1]];

最后,在访问 textfield 文本属性之前,请确保它实际上是一个文本字段并且视图存在:

NSString * textFieldText = nil;
UIView * view = [cell.contentView viewWithTag:index];
if(view && [view isKindOfClass:[UITextField class]]) {
    UITextField * textField = (UITextField *)view;
    textFieldText = [textField text];
}

if(textFieldText) {
    // Use text here...
}

【讨论】:

  • @Bangalore 很高兴,通过使用我在最后一点中编写的代码,您将 100% 避免崩溃...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多