如何使用标记动态创建UITextField(在UITableViewCell之上)文本值?

时间:2015-12-06 06:54:47

标签: ios objective-c uitableview uitextfield

我有一个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 Value但崩溃的原因。

 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;
}

2 个答案:

答案 0 :(得分:1)

cell.contentView.subviews中的第0个索引已经被名为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];

答案 1 :(得分:1)

修复你的功能的第一行:

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 text属性之前,请确保它实际上是一个文本字段并且视图存在:

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...
}