【问题标题】:How to get rect size of UITableViewCell?如何获得 UITableViewCell 的矩形大小?
【发布时间】:2011-02-10 05:00:17
【问题描述】:

如何获得 UITableViewCell 的矩形大小?

我试图在用户每次单击单元格时显示弹出悬停,并希望弹出悬停显示在每个单元格的中心:

[replyPopover presentPopoverFromRect:CGRectMake(77, 25, 408, 68) 
                              inView:self.view 
            permittedArrowDirections:UIPopoverArrowDirectionDown 
                            animated:YES];

【问题讨论】:

    标签: objective-c iphone cocoa-touch uitableview ipad


    【解决方案1】:

    最近不得不这样做。您需要使用[tableView rectForRowAtIndexPath]

    但是,可能会引起您注意的一件事是,当您滚动浏览 UITableView 时,您需要考虑偏移量(即,将其 y 位置报告为 3000 像素的东西当然意味着距顶部 3000 UITableView - 屏幕上不是 3000 像素)。

    除此之外,它自然会出现在单元格的顶部,因此需要将单元格的一半高度添加到计算中。

    这段代码对我很有效:

    CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
    CGPoint yOffset = self.tableView.contentOffset;
    [self.popoverController presentPopoverFromRect:CGRectMake(frame.origin.x, (frame.origin.y + 45 - yOffset.y), frame.size.width, frame.size.height)
    

    在我的例子中,每个单元格都是 90 像素高,因此是“+45”位。代入您自己的单元格高度的一半。

    【讨论】:

    • 这很有帮助。我擦错了,我用 [[tableView cellForRowAtIndexPath:indexPath] height] 搞砸了我的头 - 你的解决方案是我真正需要的。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2021-11-17
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    相关资源
    最近更新 更多