[英]swift: Popover on button in table view cell always display on the first cell
我正在Swift中制作一个表格视图,其中每个单元格都包含一个按钮。 轻按一个按钮时,该按钮上将显示一个弹出框。
我的问题是,无论我单击哪个单元格内部的按钮,弹出框始终显示在第一个单元格上。
请参阅附件图像以获取更多信息。
下面是我在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
。
尝试在客户UITableViewCell类中声明shareAction并将其添加到单元格。 像下面
import UIKit
class CellCustome: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@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)
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
谢谢大家的启发。
我可以通过将最后一行更改为
popover!.presentPopoverFromRect(sender.frame, inView: sender.superview!, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
现在,弹出气球显示在我触摸的正确按钮上。
但是我仍然不太了解inView参数。
如果是self.view,则弹出气球显示在第一个单元格上。 如果是sender.superview,则弹出气球显示在正确的单元格上。
Swift4更新:
在视图控制器中:
popover?.sourceView = sender.superview
popover?.sourceRect = sender.frame
self.present(popoverContent, animated: true, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.