2012-09-05 27 views
3

我有一堆水平的UITableView放在UIViewController1中。我想找到被挖掘的UITableViewCell的位置,我想根據UIViewController1獲取位置。所以這是我在didSelectRowAtIndexPath中所做的:convertRect獲取超級視圖中的相對位置

MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath]; 

       UIView *animatedView = [[UIView alloc] initWithFrame:cell.frame]; 
[animatedView setFrame:[animatedView convertRect:animatedView.frame toView:self.newsRackController.view]]; 

但是,它似乎不能正確轉換它。我究竟做錯了什麼?

+0

檢查[此答案](http://stackoverflow.com/a/4620507/442328)。 – KDaker

回答

8

您需要從UITableView的視圖中轉換UITableViewCell的CGRect,然後再將其轉換爲您的視圖控制器視圖。

但不是所有的,也許你可以只使用的UITableView的方法

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath 

得到這個的CGRect並將其轉換成你的UIViewController的視圖。

+0

謝謝,很好的回答! –

+0

對我來說,這個解決方案沒有考慮到contentOffset或contentInset。 –

1

這是因爲animatedView不屬於任何父母,並且您使用frame作爲bounds。您應該首先找到該幀並相應地初始化爲animatedView

MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath]; 
CGRect animatedViewFrame = [tableView convertRect:cell.frame toView:self.newsRackController.view]; 
UIView *animatedView = [[UIView alloc] initWithFrame:animatedViewFrame]; 
+0

謝謝!這是問題的答案。 – skywinder

相關問題