在UITableViewCell UIButton的位置Get position of UIButton in UITableViewCell

- 此内容更新于:2015-01-27
主题:

原文:

I have a button in a static UITableViewCell. What I want to do is place a UIView at the buttons location.

If I just get the buttons position, it will give me a position relative to the tableviews cell, not from the top of the tableViewController. What I want is get the buttons position from the top of the tableViewController.

For example: If the button is 8px away from the top of the current cell, but 57px away from the top of the viewController, I want to place the UIView at 57px.

Here's my code:

- (void)viewDidLoad {
    CGPoint buttonPosition = [button convertPoint:CGPointZero fromView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

    CGRect rect = [myTableView rectForRowAtIndexPath:indexPath];

    NSLog(@"%f, %f", rect.origin.y, rect.origin.x);

    self.infoView = [[UIView alloc] initWithFrame:CGRectMake(button.frame.origin.x, button.frame.origin.y, 220, 110)];
}

The output of the nslog is 0.

I would also like to update the UIView's position whenever the iphone goes to landscape mode.

Ashish Kakkad的回复:这是一个定制的细胞?

(原文:this is a custom cell?)

Mike Rally的回复:这# 39;静态细胞

(原文:It's a static cell)

Ashish Kakkad的回复:你有按钮的实例吗?

(原文:do you have an instance of button?)

Mike Rally的回复:按钮调用数组的按钮一样:UIButton *按钮=[自我。allButtons objecAtIndex:1);

(原文:button gets called from an array of buttons like so: UIButton *button = [self.allButtons objecAtIndex:1];)

Demosthese的回复:你的Y位置之前添加的行数相乘的结果行按钮和行高。但我怀疑这人# 39;年代一个更好的方法来做任何你# 39;想做。告诉我们,你的真正的目标是什么?

(原文:To your Y position add the result of multiplying the number of rows before the row with the button and the rows height. But I suspect there's a better way to do whatever you're trying to do. So tell us, what your real goal is?)

解决方案:
试试这个的绝对坐标: 这会让你的origin-coordinates按钮。高度/宽度是相对的
原文:

Try this to get the absolute coordinates:

[self.view convertPoint:button.frame.origin toView:[UIWindow keyWindow]];

This should give you the origin-coordinates of your button. The height/width are relative anyway

Mike Rally的回复:它给了我一个错误在keyWIndow

(原文:It gives me an error at keyWIndow)