【问题标题】:Cannot specify rect for popover from tableviewcell无法从 tableviewcell 为弹出框指定 rect
【发布时间】:2014-09-21 14:15:06
【问题描述】:

我以编程方式设置了一个弹出框,并尝试将其呈现在 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

使用以下代码:

popoverView.popoverContentSize = CGSizeMake(580, 760);
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[popoverView presentPopoverFromRect:cell.bounds inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

这很好用,但是,它忽略了大小,因此视图被截断,因为单元格位于屏幕中间附近。当我将其更改为 (UIPopoverArrowDirectionRight|UIPopoverArrowDirectionLeft) 时,它也会忽略这一点并向上或向下显示弹出框。

我想要的是将弹出框显示在靠近单元格左侧的位置,以便它有空间向侧面打开,但是每当我尝试编辑矩形时,表格视图都会出现很多错误它是从...显示的,我尝试了很多不同的方式,例如:

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect frame = CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, cell.bounds.size.width/4, cell.bounds.size.height);
[popoverView presentPopoverFromRect:frame inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

无论我做什么,除非我使用第一个代码(FromRect:cell.bounds inView:tableView),否则我会收到以下两个错误:

-[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:8357*

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“未设置 UITableView 数据源”*

但是绝对设置了 tableview 数据源,当我尝试更改弹出框的显示区域时,我只会收到这些错误。我还尝试手动计算单元格的位置以获取框架而不是使用单元格,也就是将单元格的高度乘以 indexPath.row 的 y 值...

关于我为什么会收到这些错误的任何想法?或者我如何才能从表格视图中正确显示完整的弹出框?

编辑:此外,我获取单元格并根据单元格的边界创建框架没有问题。错误来自的行始终位于“presentPopoverFromRect:”行上。

解决方案:我终于能够使用 CGRectOffset 实现不从单元格中心弹出的预期效果,而不会出现任何错误:

CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:tableView];
[popoverView presentPopoverFromRect:CGRectOffset(rect, -400, 0) inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];

另一个编辑: 真正奇怪的是该代码适用于我测试它的第一部分。但我的表格视图有 2 个部分,出于某种原因,弹出框在第二部分工作正常部分,但我仍然收到第一部分中的错误?关于错误的任何想法??

【问题讨论】:

    标签: ios ipad uitableview uipopovercontroller


    【解决方案1】:

    使用tableview的rectForRowAtIndexPath方法获取rect

    试试这个,

             CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath]
                                          toView:tableView];
    
            [popoverController presentPopoverFromRect:rect
                                               inView:tableView
                             permittedArrowDirections:UIPopoverArrowDirectionAny
                                             animated:YES];
    

    【讨论】:

    • 这在我没有弄乱矩形时有效,但是当我尝试编辑它时,我得到了同样的两个错误。我刚刚添加了:“ CGRect frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/4, rect.size.height); ”。
    【解决方案2】:

    您的代码示例不太正确:

    [popoverView presentPopoverFromRect:cell.bounds inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];
    

    请记住,cell.bounds 的原点是 (0, 0),即bounds 矩形是相对于单元格坐标系的。所以,inView 参数应该是cell

    [popoverView presentPopoverFromRect:cell.bounds inView:cell permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];
    

    或者,您可以使用cell.frame 在表格视图的坐标系中指定单元格的矩形:

    [popoverView presentPopoverFromRect:cell.frame inView:tableView permittedArrowDirections:(UIPopoverArrowDirectionAny) animated:YES];
    

    希望这能让你走上正确的道路。

    更新

    另外,同样的错误也被报告并解决了in this question

    【讨论】:

    • 我明白你在说什么......我实际上只是混合了框架/边界,因为我一直在尝试所有我能想到的方法。但问题是我想编辑 cell.bounds 或 cell.frame 以使框架的中心偏向一侧,而当我使用编辑过的矩形时,我会出错。这些中的任何一个都可以让我从牢房的中心弹出一个窗口,但我需要将它放在一边以适应整个视图。
    • 您链接到的问题对我没有帮助,因为表格视图是通过带有自动布局的情节提要设置的,并且对该问题的回答是关于何时添加子视图?我不确定如何在这里应用它?
    • 不知道。您只提供了 3 行代码,这并不多。
    • 好吧,我发布了唯一影响错误的代码行,它总是出现在 presentPopoverFromRect 行上..
    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多