gpt4 book ai didi

ios - UITextField 在 tableviewcell 上单击显示键盘

转载 作者:行者123 更新时间:2023-11-29 01:04:57 25 4
gpt4 key购买 nike

我有一个简单的自定义表格 View 单元格,其中有一个标签和一个文本字段。在 Storyboard中看起来像这样:

enter image description here

我想在用户单击单元格中的任意位置(包括单击标签时)显示键盘。我 99% 确定实现此目的的方法是在单击单元格时调用BecomeFirstResponder。

这是我的简单 ViewController:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.tableView.dataSource = self;
self.tableView.delegate = self;

[self.tableView setEstimatedRowHeight:44.0f];
[self.tableView setRowHeight:UITableViewAutomaticDimension];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custom"];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
BOOL firstResponder = [cell becomeFirstResponder];

}

还有我的自定义表格 View 单元格:

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (BOOL) canBecomeFirstResponder {
return YES;
}

- (BOOL) becomeFirstResponder {
return [self.textField becomeFirstResponder];
}

@end

我验证了 BebeFirstResponder 被调用,但是返回 false。我错过了什么?

最佳答案

正如@alex-i 在评论中指出的那样思考 here :

This [text field not becoming first responder] can also occur when the textfield is briefly removed from the view/window hierarchy while becoming the first responder (e.g. reloading a UITableView or UICollectionView that holds that textfield).

这将在选择时发生。

除了使用 didSelectRowAtIndexPath 之外,您还可以通过以下操作将 UITapGestureRecognizer 添加到您的 tableView:

- (IBAction)tap:(UITapGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
BOOL firstResponder = [cell becomeFirstResponder];
}

它将成为第一响应者。

关于ios - UITextField 在 tableviewcell 上单击显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com