2010-05-12 82 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

相關問題