2010-05-12 84 views
3

首先我想说我对ipad/ipod/iphone开发非常陌生,对于objective-c也是如此。这就是说,我试图开发一个针对iPad的小应用程序,使用Xcode和IB,基本上,我有一个表,为表中的每个UITableViewCell添加一个按钮,该按钮包含一个包含一个图像。如何从UITableViewCell.accessoryView调用PopOver控制器?

下面是代码:

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的按钮一酥料饼的控制出现。

我想这对中的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]; 

这段代码的问题是,它显示了酥料饼在应用程序中查看的顶部,在调试时,我看到的值“矩形”,他们分别是:

x = 267 
y = 13 

所以我认为这是很明显的原因正在显示酥料饼这么涨的看法,所以我的问题是,我怎么能得到正确的价值观为酥料饼刚刚出现低于ce的附件视图上的按钮二?另外,正如你所看到的,我告诉它使用“cell.accessoryView”作为“inView:”属性,这样好吗?

回答

8

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

另请注意,如果单元靠近屏幕底部,则弹出窗口可能不会以正确的大小显示,因为您正在向上方向强制箭头。你应该手动处理或者使用Any。

+0

千恩万谢,说完全成功了! – Vic 2010-05-13 14:17:08

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

更改inView:cell.accessoryView到inView:细胞

1

这里是为迅速:

let cell = tableView.cellForRowAtIndexPath(indexPath) 
let rect = tableView.convertRect(tableView.rectForRowAtIndexPath(indexPath), toView: tableView) 


    var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("loadZone") as! UIViewController 
    var nav = UINavigationController(rootViewController: popoverContent) 

    nav.modalPresentationStyle = UIModalPresentationStyle.Popover 

    var popover = nav.popoverPresentationController 

    popoverContent.preferredContentSize = CGSizeMake(100,200) 
    popover!.sourceView = tableView 
    popover!.sourceRect = rect 

    self.presentViewController(nav, animated: true, completion: nil) 

enter image description here

相关问题