ios - swift:表格 View 单元格中按钮上的弹出窗口始终显示在第一个单元格上

标签 ios swift uitableview uipopovercontroller

我正在用 Swift 制作一个表格 View ,其中每个单元格都包含一个按钮。 点击按钮时,该按钮上会显示一个弹出框。

我的问题是,无论我点击哪个单元格里面的按钮,popover 总是显示在第一个单元格上。

请参阅附图以获得更多理解。

table popover

下面是我在 tableviewcontroller 类中的代码。我使用标签来检测按钮上的触摸。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "Cell01"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? UICell01
        //when tap share button
        cell!.shareButton.tag = indexPath.row
        cell!.shareButton.addTarget(self, action: "shareAction:", forControlEvents: .TouchUpInside)    
        return cell!
    }






    @IBAction func shareAction(sender: UIButton)    {
        //Create Action Sheet to be menu
        let alert:UIAlertController = UIAlertController(title: "Share on", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
        {
            UIAlertAction in
        }
        // Add the actions
        alert.addAction(cancelAction)

        // Present the controller
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(alert, animated: true, completion: nil)
        }
        else    //iPad
        {
             var popover:UIPopoverController?=nil
            popover = UIPopoverController(contentViewController: alert)
            popover!.presentPopoverFromRect(sender.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }

我认为问题出在最后一行。传递给函数的“发件人”仅是第一个单元格中的按钮。我该如何解决这个问题?

最佳答案

您引用的是相对于其自身单元格的 sender.frame,而不是相对于 UITableView 合并单元格的框架。检查发件人的 superview

关于ios - swift:表格 View 单元格中按钮上的弹出窗口始终显示在第一个单元格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37266361/

相关文章:

ios - 通过数据创建 UIImage 时崩溃

ios - UITableView 中具有动态高度的可拉伸(stretch)标题 View

Android Xamarin 使推送通知不创建新 Activity 而是使用当前 Activity

ios - 从 iPhone 应用程序更新歌曲/轨道信息

ios - 计算距离另一对纬度/经度坐标特定距离的纬度/经度坐标

ios - 我如何为 Swift Realm 对象定义基类?

ios - 为什么我保存在目录中的图像在安装新版本后不显示?

ios 后台应用程序从 MPNowPlayingInfoCenter 响应缓慢

ios - 显示单元格部分会导致 iOS 应用程序崩溃

ios - 将 subview 添加到自定义类