2

我很难从 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];

}
4

3 回答 3

2

这对于具有以下内容的单元格非常有效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)

测试标记

于 2017-09-27T18:40:07.693 回答
1

此线程中的这段代码帮助了我:How to correct present a popover from a UITableViewCell with UIPopoverArrowDirectionRight 或 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; 
    }
}
于 2013-05-25T18:12:56.327 回答
1

很快,这对我有用:

创建一个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

                }
            }
        }
    }
}
于 2015-06-16T01:44:24.517 回答