0

我在 UITableViewCell 中有一个 UITextField,我想检测用户何时开始在 UITextField 中输入内容。所以我设置了 UITextFields 委托,但是当该代码运行时,我收到警告“设置表的第一响应者视图但我们不知道它的类型(单元格/页眉/页脚)”并且 UITextField 委托方法不会被调用. 如何正确获取 UITextField 方法以被调用?

-(void)tableView:(id)view willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ((indexPath.section == 0) && (indexPath.row == 3)) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(friendCellTapped)];
        [tap setNumberOfTapsRequired:1];
        [cell addGestureRecognizer:tap];
    }
    else if ((indexPath.section == 0) && (indexPath.row == 4)) {
        NSLog(@"im called");
        UITextField *tagBox = [[UITextField alloc] initWithFrame:CGRectMake(50, 6, 225, 25)];
        [tagBox setBorderStyle:UITextBorderStyleRoundedRect];
        [tagBox setReturnKeyType:UIReturnKeyDone];
        tagBox.delegate = self;
        [cell addSubview:tagBox];
    }
    else {
        %orig;
    }
}
4

1 回答 1

0

我之前通过UITextField在我的类上创建一个变量UIViewController并将其设置为 UITableViewCell 中的文本字段来完成此操作tableView:cellForRowAtIndexPath。然后我将UITextField变量的委托设置在viewDidAppear:.

像这样的东西:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.theTextField.delegate = self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   //load custom cell

   self.theTextField = myCustomCell.cellTextField;

   return myCustomCell;
}

然后你的 UITextFieldDelegate 方法应该被调用。

于 2013-07-31T20:15:05.613 回答