UITableViewCell&中的UIButton越界/帧 [英] A UIButton in a UITableViewCell & getting bounds/frame

查看:54
本文介绍了UITableViewCell&中的UIButton越界/帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView的UITableView单元格中有一个UIButton.结果是一个带有按钮的列表.当按下UIButton时,它将调用touchUpInside方法,一切都很好.我遇到的问题是,我想在按下按钮时从按钮的位置(框架)缩小新视图.如果我刚刚得到UIButton的界限

I have a UIButton in a UITableView cell, in a UITableView. The result is a list with buttons in it. When the UIButton is pressed, it calls a touchUpInside method, and all is great. What I am having problems with is that I want to zoom out a new view from the location (frame) of the button when it is pressed. If I just get the bounds of the UIButton

origButton.frame

它位于UITableCell的坐标中,例如3,50,40,25.列表中的所有按钮都是相同的,因此无论列表中的哪个按钮被按下,我都会得到这些坐标.现在,如果我实际上掌握了视图的坐标

it is in the coordinates of the UITableCell, something like 3,50,40,25. This is the same for all buttons in the list, so no matter which button in the list gets pressed, then I get these coordinates. Now, if I actually grab the coordinates of my view

self.frame

当调用touchUpInside方法时,我得到了更多可用的东西.然后,我得到了单元格的坐标.伟大的!!因此,我可以从UIButton缩小新视图.太好了!

when the touchUpInside method is called, I get something more usable. I then get the coordinates of my cell. Great!! So I can zoom out a new view from the UIButton. Great!!

但这是问题所在:如果您向上滚动列表,请点击按钮,视图的缩放不是从按钮而是从屏幕下方的位置缩小.问题是,在滚动之前,我拥有的坐标是该单元格的坐标.实际上,所有单元格的所有坐标都反映了它们在表中的实际位置,只有第一个可见集的坐标可用.滚动没有更改这些坐标.

But here's the problem: If you scroll up the list, tap the button, the view zooms out not from the button but from a location below the screen. The problem is, that the coordinates I have are for the cell, BEFORE it is scrolled up. In fact all coordinates for all cells reflect their actual locations in the table, with only the coordinates of the first visible set usable. Scrolling has not changed these coordinates.

我想也许我可以使用超级视图的坐标,但是这些没有帮助.

I thought maybe I could use the super view's coordinates, but those are of no help.

任何人都知道如何获取表中列出的按钮(或视图或任何其他项目)的准确坐标,还是不可能?

Anyone know how to get accurate coordinates for buttons (or views or any other items) listed in a table, or is it not possible?

这是我如何计算新视图的中心点:

Here is how I calculate the center point of my new view:

floatingView.center = CGPointMake(origButton.frame.origin.x + 
                                  origButton.frame.size.width / 2,
                         self.frame.origin.y + self.superview.frame.origin.y);

谢谢!

推荐答案

更新:

请记住,UITableView是scrollView.因此,它的contentView嵌入在tableView(一个scrollView)中.

Remember the a UITableView is a scrollView. So it has contentView embedded within the tableView (a scrollView).

您正在接收的框架是正确的,但它并不是您想要的视图框架的参考.取而代之的是,框架引用了contentView.滚动时,单元格不会更改对contentView.frame的引用.但是,当您滚动时(根据contentOffset的数量),contentView.frame确实会参照tableView的框架进行更改.

The frame you are receiving is correct, but it is not in reference to the view frame you want. Instead the frame is in reference to the contentView. As you scroll, the cells do not change in reference to contentView.frame. However, contentView.frame DOES change in reference to the frame of the tableView as you scroll (by the amount of the contentOffset).

因此,这留下了2个选项:

So this leaves 2 options:

使用以下方法之一将单元格的框架从contentView的引用转换为tableView:

Convert the frame of the cell from reference of the contentView to the tableView using one of the following:

 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view

 - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view

或者,手动从框架中减去contentOffset.

Or, manually subtract off the contentOffset from the frame.

我推荐第一种方法.

原始答案:

我不确定您是继承UITableViewCell还是从UITableViewController执行此代码.

I'm not sure if you subclassed UITableViewCell or are performing this code from the UITableViewController.

但是,我认为您想要的方法是:

However, I think the method you want is:

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath

这是UItableView的一种方法,可让您获得任何单元格的矩形.

This is a method of UItableView that will get you the rect for any cell.

这篇关于UITableViewCell&中的UIButton越界/帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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