我正在用 Swift 制作一个表格 View ,其中每个单元格都包含一个按钮。 点击按钮时,该按钮上会显示一个弹出框。
我的问题是,无论我点击哪个单元格里面的按钮,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/