Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何将UIAlertController操作表锚定到特定单元格UICollectionView?_Swift - Fatal编程技术网

Swift 如何将UIAlertController操作表锚定到特定单元格UICollectionView?

Swift 如何将UIAlertController操作表锚定到特定单元格UICollectionView?,swift,Swift,尝试将警报的popoverPresentationController锚定到特定的UICollectionViewCell时,我发现它只锚定到UINavigationController 我需要采取哪些步骤以编程方式触发此警报控制器并将其锚定到用户选择的单元格 override func collectionView(collectionView:UICollectionView,didSelectItemAt indepath:indepath){ 让selectedCell=collect

尝试将警报的
popoverPresentationController
锚定到特定的
UICollectionViewCell
时,我发现它只锚定到
UINavigationController

我需要采取哪些步骤以编程方式触发此警报控制器并将其锚定到用户选择的单元格

override func collectionView(collectionView:UICollectionView,didSelectItemAt indepath:indepath){
让selectedCell=collectionView.DequeueReuseAbleCell(withReuseIdentifier:PersonCell.reuseIdentifier,for:indexPath)
让person=people[indexPath.item]
let alert=UIAlertController(标题:nil,消息:nil,首选样式:。操作表)
addAction(UIAlertAction(标题:“删除”,样式:。破坏性,处理程序:{…}))
addAction(UIAlertAction(标题:“重命名”,样式:。默认,处理程序:{…}))
alert.addAction(UIAlertAction(标题:“取消”,样式:。取消))
alert.modalPresentationStyle=.popover
alert.popoverPresentationController?.sourceView=selectedCell
alert.popoverPresentationController?.sourceRect=CGRect(x:selectedCell.bounds.maxX,y:selectedCell.bounds.midY,宽度:0,高度:0)
当前(警报、动画:真)
}

您的代码的问题是,您正在将可重用单元出列,而不是将该单元放在适当的位置。
dequeueReusableCell
将在未准备好缓存副本的情况下创建一个新单元。在您的例子中,这是创建一个具有
大小为零
帧零(x=0,y=0)
的单元格,因此指针指向该单元格

您应该使用
collectionView.cellForItem(位于:indepath)

因此,最终的代码将类似于下面的代码

if let selectedCell = collectionView.cellForItem(at: indexPath) {
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { ... }))
    alert.addAction(UIAlertAction(title: "Rename", style: .default, handler: { ... }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))

    alert.modalPresentationStyle = .popover
    alert.popoverPresentationController?.sourceView = selectedCell
    alert.popoverPresentationController?.sourceRect = CGRect(x: selectedCell.bounds.midX, y: selectedCell.bounds.maxY, width: 0, height: 0)
    present(alert, animated: true)
}
这是我的样本的输出


添加用于显示警报控制器的代码。您是否尝试过@PGDev添加我当前使用的代码,该代码似乎与documentation@Cerlin是的,但是谢谢你的链接;我还没有看到那条线,但它基本上说的都是我尝试过的东西。我还尝试安全地展开属性和内部设置。如我提供的链接中所述,
警报。如果模式演示样式不是
popover
,则popoverPresentationController
将为零。在访问
popoperpresentationcontroller
之前,执行
alert.modalPresentationStyle=.popover
D'oh!那是票。谢谢你@Cerlin!