Cod*_*Guy 47 iphone uitableview ios
鉴于UITableView,我怎样才能找到具体的位置UITableViewCell?换句话说,我想得到它相对于我的iPhone屏幕的框架,而不是相对于UITableView.因此,如果我UITableView向上滚动,UITableViewCell屏幕上的每个位置应该更高,等等.
Jha*_*iya 95
您还可以使用该rectForRowAtIndexPath方法UITableView通过发送indexPath来获取a的位置.
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
所以使用如下:
CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
Tom*_*asz 59
除了rectForRowAtIndexPath,您还需要考虑滚动.
试试这段代码:
// Get the cell rect and adjust it to consider scroll offset
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
cellRect = CGRectOffset(cellRect, -tableView.contentOffset.x, -tableView.contentOffset.y);
Run Code Online (Sandbox Code Playgroud)
Vla*_*mir 22
尝试以下(将nil作为toView参数发送意味着您要将rect转换为窗口坐标):
CGRect r = [cell convertRect:cell.frame toView:nil];
Run Code Online (Sandbox Code Playgroud)
请记住,如果特定行当前不可见,那么可能没有UITableViewCell - 所以在使用该代码之前,您可能需要检查单元格是否有效(例如,不是nil)
Wil*_* Hu 11
斯威夫特3
相对于tableView:
let rect = self.tableView.rectForRow(at: indexPath)
Run Code Online (Sandbox Code Playgroud)
相对于Screen:
如果你只知道cell,
if let indexPath = tableView.indexPath(for: cell) {
let rect = self.tableView.rectForRow(at: indexPath)
let rectInScreen = self.tableView.convert(rect, to: tableView.superview)
}
Run Code Online (Sandbox Code Playgroud)
如果你知道indexPath那么就不需要调用if语句了.
小智 9
尝试一下
didSelectRowAtIndexPath 方法
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// get current location of selected cell
CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]];
NSLog(@"Cell Y Is %f",rectInSuperview.origin.y);
NSLog(@"Cell X Is %f",rectInSuperview.origin.x);
Run Code Online (Sandbox Code Playgroud)
Jhaliya 的回答对我来说还不够,我需要做更多的操作才能让它工作。我的 tableView 被添加到一个 viewController 中,它的位置在屏幕下方的右侧。因此,您需要考虑 tableView 原点以及滚动偏移量。
CGRect rowRect = [tableView rectForRowAtIndexPath:indexPath];
CGPoint offsetPoint = [self.infoTableView contentOffset];
// remove the offset from the rowRect
rowRect.origin.y -= offsetPoint.y;
// Move to the actual position of the tableView
rowRect.origin.x += self.infoTableView.frame.origin.x;
rowRect.origin.y += self.infoTableView.frame.origin.y;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52961 次 |
| 最近记录: |