簡體   English   中英

如何使用標簽獲取動態創建的UITextField(在UITableViewCell之上)文本值?

[英]How to get dynamically created UITextField (on top of UITableViewCell ) text value using tag?

我有一個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;
}

cell.contentView.subviews中的第零個索引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];

將函數的第一行固定為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM