【问题标题】:Present popover from cells accessory button?从单元格附件按钮呈现弹出窗口?
【发布时间】:2013-05-21 01:19:04
【问题描述】:

我很难从 tableviewCell 的附件按钮正确显示弹出框。

我不使用附件视图的原因是单元格处于编辑模式,我无法同时显示绿色加号 + 自定义附件视图。也许我忽略了前面的某些内容?

目前我的弹出框显示正确,但这只是这种配置的情况,因为我设置了与原点的静态距离......有什么想法可以解决这个问题吗?

代码:

-(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];

}

【问题讨论】:

  • 可能你会考虑 cell.accessoryView.bounds 或直接考虑 cell.accessoryView.frame
  • 非常感谢您的建议。我试过: CGRect rect = CGRectMake(cell.accessoryView.bounds.origin.x, cell.accessoryView.bounds.origin.y, 50, 30);但这在 x 方面不起作用。也许是因为我实际上并没有加载附件视图?还是我错过了什么?
  • cell.accessoryView.bounds.size.width-50 尝试在 CGRectMake 第一个参数中使用这个
  • 再次感谢您的建议 tirupathi,没有解决。瑞秋的提示帮助了我,谢谢!!我以某种方式认为附件视图无效,因为我使用了附件类型。吸取教训!

标签: ios ipad popover cgrect uitableview


【解决方案1】:

这个线程中的这段代码帮助了我:How to correctly present a popover from a UITableViewCell with UIPopoverArrowDirectionRight or UIPopoverArrowDirectionLeft 感谢瑞秋的提示

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; 
    }
}

【讨论】:

    【解决方案2】:

    很快,这对我有用:

    创建一个popoverpresentation 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 is UIButton {
                                accessoryView = accView as? UIButton
                                break
                            }
                        }
                        popOverPresentationController.delegate = self
                        popOverPresentationController.sourceView                = cell
                        popOverPresentationController.sourceRect                = accessoryView.frame
                        popOverPresentationController.permittedArrowDirections  = UIPopoverArrowDirection.Right
    
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这对于带有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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 2021-11-11
        • 2022-10-01
        • 1970-01-01
        • 2021-05-19
        • 2023-04-09
        • 1970-01-01
        相关资源
        最近更新 更多