- (IBAction)textFieldDone:(id)sender { //[sender resignFirstResponder]; UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];//sender是文本字段,它是表单元视图的内容视图的一个子视图,[cell.contentView addSubview: textField]; UITableView *table = (UITableView *)[cell superview]; NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell]; NSUInteger row = [textFieldIndexPath row]; row++; if (row >= kNumberOfEditableRows) { row = 0; } NSIndexPath *newPath = [NSIndexPath indexPathForRow:row inSection:0]; UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:newPath]; UITextField *nextField = nil; for (UIView *oneView in nextCell.contentView.subviews) { if ([oneView isMemberOfClass:[UITextField class]]) { nextField = (UITextField *)oneView; } } [nextField becomeFirstResponder]; }
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(90, 12, 200, 25)]; textField.clearsOnBeginEditing = NO;//鼠标点上时,不清空 [textField setDelegate: self]; //textField.returnKeyType = UIReturnKeyDone; [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];//把DidEndOnExit事件响应为 textfieldDone: 方法 [cell.contentView addSubview: textField];