获取单元格或文本字段在此单元格中的位置 [英] Get position of cell or text field in this cell

查看:34
本文介绍了获取单元格或文本字段在此单元格中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拆分视图控制器:view &表视图.在这个表格视图中,我有带有文本字段的自定义单元格.我不知道我会有多少个单元格,所以它会自动生成.现在我正在尝试滚动到文本字段,当它成为FirstResponder 时.我试过这样的事情:

I have a split view controller: view & tableview. In this table view I have custom cells with text field. I don't know how many cells will I have, so it generate automatically. And now I'm trying scroll to textfield, when it becomeFirstResponder. I've tried something like this:

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    CGPoint focusOnTextField = CGPointMake(0, 300 + textField.frame.origin.y);
    [scroller setContentOffset: focusOnTextField animated: YES];
}

300px - 我的 TableView 的起始位置.一切似乎都很好,但 textField.frame.origin.y 总是等于 0(就像 bounds.origin.y 顺便说一句).

300px - start position of my TableView. All seems alright, but textField.frame.origin.y always equal to 0 (like and bounds.origin.y btw).

我想如果获取单元格的位置,哪个文本字段处于活动状态,然后在 cell.frame.origin.y<上替换 textField.frame.origin.y ,我想我可以解决问题/code> 或类似的东西.

I thought I can solve a problem if get position of cell, which textfield is active, and then replace textField.frame.origin.y on cell.frame.origin.y or something like this.

====================================================================

我忘了说我的 tableviews 滚动已禁用.我按照你的建议和代码示例并像这样解决它:

I forgot say that my tableviews scroll is disabled. I follow your advices and code examples and solve it like that:

- (UITableViewCell *)cellWithSubview:(UIView *)subview {

    while (subview && ![subview isKindOfClass:[UITableViewCell self]])
        subview = subview.superview;
    return (UITableViewCell *)subview;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UITableViewCell *activeCell = [self cellWithSubview:textField];

    float offsetValueY = 200 + activeCell.origin.y;
    CGPoint focusOnTextField = CGPointMake(0, offsetValueY);
    [scroller setContentOffset:focusOnTextField animated:YES];
}

知道吗?它正在工作!:-) 但它产生了一个新问题.当我开始编辑文本字段时,滚动条首先跳到顶部,然后才转到正确的位置.当我写 [scroller setContentOffset:focusOnTextField animation:NO]; 时,这个问题消失了,但滚动条没有平滑移动.这对我来说很糟糕:-) 那么我们如何解决它?

And know what? It's working! :-) But it create a new problem. When I begin editing textfield, scroller at first jump on top, and only then going to it correct position. When I write [scroller setContentOffset:focusOnTextField animated:NO]; and this problem disappear, but there is no smooth move of scroller. And this is bad to me :-) So how we can solve it?

推荐答案

这里是如何滚动到包含文本字段的单元格...

Here's how to scroll to a cell containing a text field ...

// find the cell containing a subview.  this works independently of how cells
// have been constructed.

- (UITableViewCell *)cellWithSubview:(UIView *)subview {

    while (subview && ![subview isKindOfClass:[UITableViewCell self]])
        subview = subview.superview;
    return (UITableViewCell *)subview;
}

您的想法是在编辑开始时触发该操作是正确的.只需使用单元格即可...

Your idea is correct to trigger that action when editing begins. Just do it using the cell...

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    // find the cell containing this text field
    UITableViewCell *cell = [self cellWithSubview:textField];

    // now scroll using that cell's index path as the target
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

这篇关于获取单元格或文本字段在此单元格中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆