【问题标题】:Get position of cell or text field in this cell获取此单元格中单元格或文本字段的位置
【发布时间】:2013-05-08 23:48:28
【问题描述】:

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

-(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 顺便说一句)。

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

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

我忘了说我的 tableviews 滚动被禁用了。我遵循您的建议和代码示例并像这样解决它:

- (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 animated:NO]; 并且这个问题消失了,但是滚动条没有平滑移动。这对我很不利 :-) 那么我们该如何解决呢?

【问题讨论】:

    标签: ios objective-c uitableview uitextfield


    【解决方案1】:

    以下是滚动到包含文本字段的单元格的方法...

    // 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;
    }
    

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

    - (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];
    }
    

    【讨论】:

      【解决方案2】:
      [tableView scrollToRowContainingComponent:textField atScrollPosition: UITableViewScrollPositionMiddle animated:YES];
      

      在 UITableView 中添加以下类别后:

      @implementation UITableView (MyCategory)
      
      -(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
          if([component isDescendantOfView:self] && component != self) {
              CGPoint point = [component.superview convertPoint:component.center toView:self];
              return [self indexPathForRowAtPoint:point];
          }
          else {
              return nil;
          }
      }
      
      -(void)scrollToRowContainingComponent:(UIView*)component atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
      
          NSIndexPath *indexPath = [self indexPathOfCellComponent:component];
          if(indexPath) {
              [self scrollToRowAtIndexPath:indexPath atScrollPosition: scrollPosition animated:animated];
          }
      }
      
      @end
      

      【讨论】:

        【解决方案3】:

        如果您在UITableVieCell 内容视图中添加文本字段(如果您使用.xib,则默认添加),那么您必须调用类似textField.superview.superview 的内容,这将为您提供父单元格。如果将文本字段直接添加到单元格视图中,则必须使用 textField.superview

        【讨论】:

          猜你喜欢
          • 2011-06-27
          • 2013-06-07
          • 2011-10-01
          • 2012-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-07
          • 2010-10-14
          相关资源
          最近更新 更多