2013-05-25 48 views
0

我很難從tableviewCell的accessoryButton正確呈現popover。從細胞配件按鈕出現彈出?

我不使用配件視圖的原因是因爲該單元處於編輯模式,我無法同時顯示綠色加號+自定義附件視圖..也許我忽略了該前端的某些內容?

目前我的彈出窗口顯示正確,但這只是這種配置的情況,因爲我設置了一個靜態的距離...任何想法如何解決這個問題?

代碼:

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

if (![self duplicateDayContent]) { 
    duplicateDayContent = [[self storyboard]instantiateViewControllerWithIdentifier:@"CopyDay"]; 
    [duplicateDayContent setDelegate:self]; 

    duplicateDayPopover = [[UIPopoverController alloc]initWithContentViewController:duplicateDayContent]; 
    duplicateDayPopover.popoverContentSize = CGSizeMake(320, 600); 

} 

CGRect rect = CGRectMake(cell.bounds.origin.x+800, cell.bounds.origin.y+10, 50, 30); 

[duplicateDayPopover presentPopoverFromRect:rect inView:cell permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; 

} 
+1

可能你會考慮cell.accessoryView.bounds或直接考慮cell.accessoryView.frame –

+0

thx很多你的建議。我試過了:CGRect rect = CGRectMake(cell.accessoryView.bounds.origin.x,cell.accessoryView.bounds.origin.y,50,30); 但是這在x方面不起作用。也許是因爲我實際上沒有加載輔助視圖?或者我錯過了什麼? – L00ps

+0

cell.accessoryView.bounds.size.width-50嘗試使用此CGRectMake第一個參數 –

回答

1

從這個線程的代碼幫我:How to correctly present a popover from a UITableViewCell with UIPopoverArrowDirectionRight or UIPopoverArrowDirectionLeft 感謝rachels暗示

UIView *accessoryView  = cell.accessoryView; // finds custom accesoryView  (cell.accesoryView) 
if (accessoryView == nil) { 
    UIView *cellContentView = nil; 

    for (UIView *accView in [cell subviews]) { 
     if ([accView isKindOfClass:[UIButton class]]) { 
      accessoryView = accView; // find generated accesoryView (UIButton) 
      break; 
     } else if ([accView isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) { 
      // find generated UITableViewCellContentView 
      cellContentView = accView; 
     } 
    } 
    // if the UIButton doesn't exists, find cell contet view (UITableViewCellContentView) 
    if (accessoryView == nil) { 
     accessoryView = cellContentView; 
    } 
    // if the cell contet view doesn't exists, use cell view 
    if (accessoryView == nil) { 
     accessoryView = cell; 
    } 
} 
0

在迅速,這爲我工作:

創建酥料餅演示文稿segue,然後使用prepareForSegue配置目標視圖控制器的UIPopoverPresentationController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == Storyboard.providerInfoSegue) { 
     if let vc = segue.destinationViewController.contentViewController as? /*DestinationViewControllerType*/ { 
      //Configure view controllers here 

      if let popOverPresentationController : UIPopoverPresentationController = vc.popoverPresentationController { 


       if let cell = tableView.cellForRowAtIndexPath(selectedAccessoryIndexPath) { 
        var accessoryView: UIButton! 
        for accView in cell.subviews { 
         if (accView.isKindOfClass(UIButton)) { 
          accessoryView = accView as! UIButton 
          break 
         } 
        } 
        popOverPresentationController.delegate = self 
        popOverPresentationController.sourceView    = cell 
        popOverPresentationController.sourceRect    = accessoryView.frame 
        popOverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Right 

       } 
      } 
     } 
    } 
} 
0

這個工程很好地與accessoryType .detailDisclosureButton細胞:

if let ppc = vc.popoverPresentationController, let cell = tableView.cellForRow(at: indexPath) { 
    ppc.sourceView = cell 
    ppc.sourceRect = CGRect(x: cell.bounds.width - 58, y: cell.bounds.height/2 - 11, width: 22, height: 22) 
    vc.modalPresentationStyle = .popover 
} 
present(vc, animated: true, completion: nil) 

tableView(_ accessoryButtonTappedForRowWith indexPath:)

你通常會做這個,你還可以測試計算的幀的位置標記:

let marker = UIView(frame: ppc.sourceRect) 
marker.backgroundColor = UIColor.red.withAlphaComponent(0.2) 
cell.addSubview(marker) 

Test marker