objective-c - 如何从 UITableViewCell.accessoryView 调用 PopOver Controller ?

标签 objective-c uitableview ipad uipopovercontroller

首先我想说我对 ipad/ipod/iphone 开发真的很陌生,对 objective-c 也是如此。

话虽如此,我正在尝试使用 Xcode 和 IB 开发一个针对 iPad 的小型应用程序,基本上,我有一个表,对于表中的每个 UITableViewCell,我向 accessoryView 添加了一个按钮,其中包含一个图片。

代码如下:

UIImage *img = [UIImage imageNamed:@"myimage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
button.frame = frame;   // match the button's size with the image size

[button setBackgroundImage:img forState:UIControlStateNormal];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

到目前为止一切顺利,现在的问题是我希望在用户点击单元格的 accessoryView 上的按钮时出现 PopOver 控件。

我在 tableView 的“accessoryButtonTappedForRowWithIndexPath”上试过这个:

UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;

//customViewController is the controller of the view that I want to be displayed by the PopOver controller
customViewController = [[CustomViewController alloc]init];

popOverController = [[UIPopoverController alloc]
initWithContentViewController: customViewController];

popOverController.popoverContentSize = CGSizeMake(147, 122);

CGRect rect = button.frame;

[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

这段代码的问题在于它在应用程序 View 的顶部显示了 Popover,在调试时我看到了“rect”的值,它们是:

x = 267
y = 13

所以我认为很明显为什么 PopOver 在 View 上显示得如此之高,所以我的问题是,我怎样才能让 PopOver 的正确值出现在单元格的 accessoryView 的按钮下方?

此外,如您所见,我告诉它为“inView:”属性使用“cell.accessoryView”,这样可以吗?

最佳答案

尝试使用 button.bounds 而不是 button.frame 因为 rect 是相对于 inView 的。

另请注意,如果单元格位于屏幕底部附近,则弹出框可能无法以正确的大小显示,因为您迫使箭头朝上方向移动。您应该手动处理或只使用 Any。

关于objective-c - 如何从 UITableViewCell.accessoryView 调用 PopOver Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2823342/

相关文章:

ios - Objective-C:捕获自定义框架内所有 View 的屏幕截图

ios - 具有自动布局和最大尺寸保持纵横比的 UIImageView

ios - UITableViewCell 自定义类和 UITableView 滚动问题

ios - TabBar 上方的 TableView 上的黑条

ios - 如何正确隐藏这些广告横幅?

swift、numberOfRowsInSection 和 cellForRowAt 函数不返回值

ios - UITableView 未声明类型 Cell?

iphone - xib 文件中的 iOS 自定义 View

ios - iPad iOS 6 崩溃

iphone - 无法将参数传递给 @selector 方法?