Ios 在自定义UITableViewCell中取消UITextField时出现问题
我在表视图中实现了一个自定义单元格。 在单元格内,我有一个文本字段(设置为数字键盘)。 在表视图中,我有12个单元格。 我可以用我放在自定义单元格类中的代码,用Ios 在自定义UITableViewCell中取消UITextField时出现问题,ios,uitableview,keyboard,uitextfield,dismiss,Ios,Uitableview,Keyboard,Uitextfield,Dismiss,我在表视图中实现了一个自定义单元格。 在单元格内,我有一个文本字段(设置为数字键盘)。 在表视图中,我有12个单元格。 我可以用我放在自定义单元格类中的代码,用触摸开始(尽管只有当我点击正在编辑的活动单元格时,它才起作用)来打开键盘。 如果我将键盘更改为字母和数字,也可以关闭键盘。 我在键盘上有一个自定义的“完成”按钮,我希望它关闭文本字段 我试着用endEditing:YES或[cell.textFieldAnswer resignFirstResponder]关闭键盘已有一段时间了,但它不起
触摸开始
(尽管只有当我点击正在编辑的活动单元格时,它才起作用)来打开键盘。
如果我将键盘更改为字母和数字,也可以关闭键盘。
我在键盘上有一个自定义的“完成”按钮,我希望它关闭文本字段
我试着用endEditing:YES
或[cell.textFieldAnswer resignFirstResponder]
关闭键盘已有一段时间了,但它不起作用
有什么想法吗
安全问题.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSInteger nextIndexPathRow = indexPath.row;
nextIndexPathRow++;
cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];
switch (indexPath.row) {
case 0:
tfA1 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 1:
tfA2 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 2:
tfA3 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 3:
tfA4 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 4:
tfA5 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 5:
tfA6 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 6:
tfA7 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 7:
tfA8 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 8:
tfA9 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 9:
tfA10 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 10:
tfA11 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
case 11:
tfA12 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
break;
}
cell.textFieldAnswer.tag = indexPath.row;
cell.textFieldAnswer.delegate = self;
[cell.textFieldAnswer setText:[answers objectAtIndex:indexPath.row]];
return cell;
}
GetQuestionsCustomCell.m:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.textFieldAnswer resignFirstResponder];
}
编辑1:
当我这样做时:[cellperformSelector@selector(按键:)]
我有一个裂缝。
向下键位于自定义单元视图控制器内。
注意:“单元格”被配置为我上面提到的自定义单元格
错误:
线程1:EXC\u错误访问(代码=2,地址=0x631ec)
编辑2:
感谢“Thilina”在私人聊天中的所有帮助,现在它可以工作了,修复:
- (void)doneAction:(id)sender{
NSArray *cells = [self.tableView visibleCells];
for(UIView *view in cells){
if([view isMemberOfClass:[GetQuestionsCustomCell class]]){
GetQuestionsCustomCell *cell = (GetQuestionsCustomCell *) view;
UITextField *tf = (UITextField *)[cell textFieldAnswer];
if([tf isFirstResponder]){
[tf resignFirstResponder];
}
}
}
}
1) 将代理UITextFieldDelegate添加到ViewController
@interface ViewController ()<UITextFieldDelegate>
3) 在tableView cellForRowAtIndexPath方法中更改开关
switch (indexPath.row) {
case 0:{
tfA1 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA1.delegate = self;
}
break;
case 1:{
tfA2 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA2.delegate = self;
}break;
case 2:{
tfA3 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA3.delegate = self;
}break;
case 3:{
tfA4 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA4.delegate = self;
}break;
case 4:{
tfA5 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA5.delegate = self;
}break;
case 5:{
tfA6 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA6.delegate = self;
}break;
case 6:{
tfA7 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA7.delegate = self;
}break;
case 7:{
tfA8 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA8.delegate = self;
}break;
case 8:{
tfA9 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA9.delegate = self;
}break;
case 9:{
tfA10 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA10.delegate = self;
}break;
case 10:{
tfA11 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA11.delegate = self;
}break;
case 11:{
tfA12 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA12.delegate = self;
}break;
}
customcell的textfield的属性名称是什么?请发布您的方法,-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPathok,编辑并添加。添加并更改。当我点击一个按钮时,我将“resignFirstResponder”设置为所有tfA1…12。我将日志设置为textFieldShouldReturn,但仍然没有返回任何内容。什么也没发生。只有黑暗的接触才开始起作用。另外,“textfielddidediting”在重要时调用。好的,完成按钮的属性名是什么?我可以通过自定义单元格访问按钮吗?奇怪的是,普通键盘会自动关闭,但使用数字键盘不会。当我在父单元格中时,也许我可以执行位于自定义单元格中的选择器?是的,它在普通键盘上工作,但我不需要自定义“完成”按钮。是否在.h中包含KeyDown:method声明?
switch (indexPath.row) {
case 0:{
tfA1 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA1.delegate = self;
}
break;
case 1:{
tfA2 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA2.delegate = self;
}break;
case 2:{
tfA3 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA3.delegate = self;
}break;
case 3:{
tfA4 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA4.delegate = self;
}break;
case 4:{
tfA5 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA5.delegate = self;
}break;
case 5:{
tfA6 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA6.delegate = self;
}break;
case 6:{
tfA7 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA7.delegate = self;
}break;
case 7:{
tfA8 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA8.delegate = self;
}break;
case 8:{
tfA9 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA9.delegate = self;
}break;
case 9:{
tfA10 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA10.delegate = self;
}break;
case 10:{
tfA11 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA11.delegate = self;
}break;
case 11:{
tfA12 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
tfA12.delegate = self;
}break;
}