【发布时间】:2013-04-29 08:28:23
【问题描述】:
我在表格视图中实现了一个自定义单元格。
在单元格内,我有一个文本字段(设置为数字键盘)。
在表格视图中,我有 12 个单元格。
我可以用touchesBegan 删除键盘(尽管它仅在我点击我正在编辑的活动单元格时才有效)使用我放入自定义单元格类的代码。
如果我将键盘更改为字母和数字,我也可以关闭键盘。
我在键盘上有一个自定义的“完成”按钮,我希望它可以关闭文本字段。
我一直在尝试使用 endEditing:YES 或 [cell.textFieldAnswer resignFirstResponder] 关闭键盘,但它不起作用。
有什么想法吗?
SecurityQuestions.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:
当我在做的时候:[cell performSelector@selector(KeyDown:)] 我有一个crach。
KeyDown 位于自定义单元格视图控制器内。
注意:'cell' 配置为我上面提到的自定义单元格。
错误:
线程 1:EXC_BAD_ACCESS(代码=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];
}
}
}
}
【问题讨论】:
-
customcell 的文本字段的属性名称是什么?
-
请发布您的方法,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-
好的,已编辑并添加。
标签: ios uitableview keyboard uitextfield dismiss