【发布时间】:2013-03-26 09:26:21
【问题描述】:
我目前正在尝试使其正常工作。任何帮助表示赞赏。
我有一个自定义的UITableViewCell,里面有一个UITextField。当我选择表格单元格时,我想让UITextField 第一个响应者。
但是 [textField becomeFirstResponder]; 返回 false。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
UITextField *textField;
for (UIView *view in [cell subviews]) {
if ([view isMemberOfClass:[UITextField class]]) {
textField = ((UITextField *)view);
}
}
[textField becomeFirstResponder];
}
根据要求,初始化文本字段。这是在UITableViewCell 子类中完成的:
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Init Textfield
_textField = [[UITextField alloc] init];
_textField.backgroundColor = [UIColor clearColor];
_textField.delegate = self;
[self addSubview:_textField];
}
return self;
}
【问题讨论】:
-
你的 textField==nil; ?我的意思是你能得到文本字段参考吗?
-
是的,我得到了 textField 参考。方法 becomeFirstResponder 只返回 false,因为由于某种原因无法将 textField 提升为 firstResponder。
-
请告诉我们您如何创建
UITextFields。也许您的文本字段委托方法之一返回 NO?
标签: ios objective-c uitableview uitextfield