I am making a table view in Swift which each cell contains a button. When a button is tapped, a popover will display on that button.
My Problem is, no matter which cell I click the button inside, popover always display on the first cell.
Please see attached images for more understanding.
Below is my code in tableviewcontroller class. I use tag to detect touching on the button.
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)
}
I think the problem is at the last line. "sender" that is passed to function is the button in the first cell only. How do I solve the problem?