【发布时间】: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