我现在正努力让这件事发挥作用。任何帮助都是非常感谢的。
我有一个定制的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];
}根据请求,初始化textfield。这是在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;
}发布于 2014-08-13 01:50:05
如果您有一个自定义单元格,那么您可以这样做:
@implementation CustomCell
#pragma mark - UIResponder
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)becomeFirstResponder
{
return [self.textField becomeFirstResponder];
}
@end然后在表视图委托中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell canBecomeFirstResponder]) {
[cell becomeFirstResponder];
}
}发布于 2013-04-04 10:49:02
为每个tag提供UITextField,并使用以下代码:
UITableViewCell* cell = (UITableViewCell*)sender.superview.superview;
UITextField *txtField = (UITextField*)[cell viewWithTag:tag];
[txtField becomeFirstResponder];在didSelectRowAtIndexPath方法中。
它工作得很好..。:)
发布于 2013-04-04 11:09:26
我相信你需要为你设置textField的delegate财产。
在调用textField.delegate = self;之前,将[textField becomeFirstResponder]添加到方法中
https://stackoverflow.com/questions/15808936
复制相似问题