带有 TextField 和第一响应者的自定义 TableViewCell

发布于 2024-07-29 01:27:00 字数 183 浏览 10 评论 0原文

我有一个包含 TextField 的自定义 TableView 单元格,我希望它在显示视图后立即成为第一响应者,但 [textcell.textfield comeFirstResponder] 不起作用。 我知道这是因为它是另一个类中的自定义单元格,我什至在那里尝试过但它不起作用。 有人知道如何实现这一点吗?

谢谢...

I have a custom TableView cell that contains a TextField and I want it to become the first responder as soon as the view is shown but [textcell.textfield becomeFirstResponder] does not work. I know it's because it's a custom cell in another class and I even tried it there and it didn't work. Anyone know how to pull this off?

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-08-05 01:27:00

我有类似的设置,becomeFirstResponder 似乎工作正常。

我的自定义单元格:

@interface CustomCell : UITableViewCell 
{
    IBOutlet UITextField *costField;
}

控制器类中的委托方法: justAddedItem

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellTableIdentifer = @"CellTableIdentifer";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifer];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSUInteger row = [indexPath row];
    ReceiptItem *receiptItem = [models objectAtIndex:row];
    if (receiptItem == justAddedItem)
    {
        [cell.costField becomeFirstResponder];
        justAddedItem = nil;
    }

当用户单击按钮向表中添加新行时,将设置

I have a similar setup and becomeFirstResponder seems to work fine.

My custom cell:

@interface CustomCell : UITableViewCell 
{
    IBOutlet UITextField *costField;
}

And the delegate method from the controller class:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellTableIdentifer = @"CellTableIdentifer";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifer];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSUInteger row = [indexPath row];
    ReceiptItem *receiptItem = [models objectAtIndex:row];
    if (receiptItem == justAddedItem)
    {
        [cell.costField becomeFirstResponder];
        justAddedItem = nil;
    }

justAddedItem is set when the user clicks the button to add a new row to the table.

锦欢 2024-08-05 01:27:00
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = YES;
    self.title = @"Login";

    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(doSetFirstResponder)
                                   userInfo:nil
                                    repeats:NO];
}

- (void) doSetFirstResponder {
    [_usernameField becomeFirstResponder];
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = YES;
    self.title = @"Login";

    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(doSetFirstResponder)
                                   userInfo:nil
                                    repeats:NO];
}

- (void) doSetFirstResponder {
    [_usernameField becomeFirstResponder];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文