【问题标题】:How to get the TextField position in a TableCell on iPhone?如何在 iPhone 上的 TableCell 中获取 TextField 位置?
【发布时间】:2012-12-18 03:48:51
【问题描述】:

这是 UI...(我知道它很丑...)橙色是视图,蓝色是表格,灰色是表格单元格,最后,红色是文本字段一个表格单元格。我的问题很简单,我只想知道红色文本字段的位置......似乎表格单元格可以上下移动,所以位置可以改变。所以,我不需要动态更新文本字段的位置,我只需要用户单击文本字段时的位置,有什么想法吗?谢谢。

【问题讨论】:

  • 明确一点,你想知道红色视图相对于橙色视图的位置,对吧?
  • @calvinBhai 是的,你说得对。

标签: iphone objective-c cocoa-touch uitableview uitextfield


【解决方案1】:

您可以使用可用于 UIView 或其派生类的 convetrPoint 方法

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view

在你的情况下,你需要这样做:

- (void)textFieldDidBeginEditing(UITextField *)textField 
{
     CGPoint textFieldOriginInTableView = [textFiled convertPoint:textField.frame.origin toView:tableView];
     //or if you want it relative to the orangeView, your toView should be the orangeView
}

【讨论】:

    【解决方案2】:

    我想提两件事:

    1. 如果您的 textField 在 tabelViewCell 内,则无论单元格向上或向下多少,它的位置始终相同。这是因为 textField 是 tabelViewCell 的子视图而不是视图。

    2. 您可以通过多种方式获得:

      • textFieldDidBeginEditing 委托方法上(正如你提到的,当它被触摸时,你需要它,意味着开始编辑)。

      • 或者您可以随时通过以下代码:

      //可以设置文本字段的标签

      UITableViewCell *cell = [yourTableView cellForrowAtIndexPath:yourCellIndexPath];
      UITextField *textField = (UITextField *)[cell.contentView viewWithTag:textFieldTag];
      

      //或不想设置标签,只有文本字段存在

      for (UIView *v in [cell.contentView subviews]) {
        if ([v isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)v;
       }
      }
      

    【讨论】:

      【解决方案3】:

      假设拥有视图控制器是 UITextField 的委托,您可以实现 textFieldDidBeginEditing: 方法并获取文本字段的框架,如下所示:

      - (void)textFieldDidBeginEditing(UITextField *)textField 
      {
          CGRect rect = textField.frame;
      }
      

      【讨论】:

      • UITextField 属于单元格,而不是 UIViewController。
      • 这就是 Ted 的重点。它会一直保持在那个位置上。在单元格内的框架中......
      • @TedWong 如果是单元格的子视图,则无需担心 textField 的位置。如果你想对textField做位置调整,那么在cell的layOutSubview方法中做。
      【解决方案4】:

      首先给textField添加一个唯一的标签

      yourTextField.tag = 1001;
      

      比在你想要你的文本字段的位置,简单地做

      UITextField *myTextField = [cell viewWithTag:1001];
      
      CGRect textFieldRect = myTextField.Frame;
      

      它将为您提供标记的 textField 的框架,其中包含 textField 相对于其 superView 的 x、y、width、height 参数。

      【讨论】:

        【解决方案5】:

        这个带有动​​画的代码集框架试试这个代码...

        - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.2];
            CGRect youtTextFielfFrame = textField.frame;// get position of textfield from this code
            [textField setFrame:CGRectMake(textField.frame.origin.x - 50, textField.frame.origin.y,textField.frame.size.width,textField.frame.size.height)]; /// set frame which you want here......
            [UIView commitAnimations];
            return YES;
        }
        

        【讨论】:

        • @TedWong 在这里尝试这个更新的代码,您可以轻松地使用所需的框架更改文本字段的位置,还可以获得文本字段 youtTextFielfFrame 变量的位置,如上面我定义的花花公子..希望这对您有所帮助。 .. :)
        • @TedWong 帮到你了??
        猜你喜欢
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2011-11-21
        • 1970-01-01
        相关资源
        最近更新 更多