【问题标题】:How to find the x , y position of button frame in a custom cell如何在自定义单元格中找到按钮框架的 x , y 位置
【发布时间】:2013-01-28 06:43:50
【问题描述】:

我用自定义单元格制作了一个表格视图,如下所示。

![自定义单元格][1]

![输出表视图][2]

我需要在每个单元格中使用选择器而不是滑块来设置评分

我只需要在按钮下方设置选择器位置,它会随按钮位置而变化

如何实现。

iu 尝试使用以下框架和边界,但每次都给出相同的值

   - (IBAction) buttonClicked:(UIButton *)sender
    {      
          NSLog(@"------%f  %f",sender.frame.origin.x , sender.frame.origin.y); //250, 2

         NSLog(@"------%f  %f",sender.bounds.origin.x , bounds.frame.origin.y); // 0.0 , 0.0

       picker.frame = CGRectMake(sender.bounds.origin.x , sender.bounds.origin.y, 50, 216);
}

【问题讨论】:

    标签: iphone objective-c frame cgrect custom-cell


    【解决方案1】:

    您在最后一行中使用了两次“.x”值,而不是第二次使用“.y”值。

    此外,您需要将坐标转换为显示选择器的视图的坐标。包括用于显示您的选择器的代码,我可以帮助您。

    会是这样的:

    picker.frame = [viewThatYouAreDisplayingThePickerIn convertRect:sender.frame fromView:sender.superview];
    

    【讨论】:

      【解决方案2】:

      但是,如果您想获得相对于主视图的按钮框架,请使用此:

       CustomCell *cell = (CustomCell *)[self.table cellForRowAtIndexPath:indexPath];
      
       CGRect buttonFrame = [cell.superview convertRect:cell.customButton.frame toView:self.view];
      

      【讨论】:

      • 您建议我们从哪里获得indexPath? :-)
      • 当您当时在 cellforrowatindexpath 中创建单元格时,您可以为按钮添加标签,然后使用该标签创建索引路径,如 [NSIndexPath indexPathForRow:button.tag inSection:0]
      • 嗯,这是一种方法......虽然对于他正在尝试做的事情来说,这似乎是很多工作。 :) (顺便说一句,如果您出于其他原因想使用此技术,使用表格视图的indexPathsForRowsInRect 方法要容易得多,如我之前的答案所示:stackoverflow.com/a/14352372/937822 而不是标签)。
      • 是的,你是对的 lnafziger,但要获得“viewThatYouAreDisplayingThePickerIn”,他必须有办法获取表格单元格的对象..他需要一个 indexPath 来获取表格单元格对象不是吗?
      • 哦,好吧,他可以使用superview方法是的;)
      【解决方案3】:

      按钮有一个名为locationInView的方法继承自UITouch

      Returns the current location of the receiver in the coordinate system of the given view.
      - (CGPoint)locationInView:(UIView *)view
      

      Apple Developer Reference

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        • 2010-11-05
        相关资源
        最近更新 更多