从细胞配件按钮呈现popover?

时间:2013-05-25 17:03:01

标签: ios ipad popover cgrect uitableview

我很难从tableviewCell的accessoryButton中正确呈现一个popover。

我没有使用配件视图的原因是因为单元格处于编辑模式,我无法同时显示绿色加号+自定义附件视图..也许我忽略了那个方面的东西?

目前我的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];

}

3 个答案:

答案 0 :(得分: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; 
    }
}

答案 1 :(得分:1)

在swift中,这对我有用:

创建一个popover演示文稿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

                }
            }
        }
    }
}

答案 2 :(得分:1)

对于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