Reputation: 221
I have a custom UITableViewCell which contains a UIButton. when I press the button I want to display a popover with some text. The text will vary depending on the indexPath pressed.
below is my code so far.
class CellButton: UIButton {
weak var myTable: UITableView?
weak var myCell: UITableViewCell?
}
This is my custom UITableViewCell. I have a button action that prints a line, I want to instead display this as a popover.
class CourseworkTableViewCell: UITableViewCell, UIPopoverPresentationControllerDelegate {
@IBOutlet weak var courseworkName: UILabel!
@IBOutlet weak var courseworkMark: UILabel!
@IBOutlet weak var courseworkValue: UILabel!
@IBOutlet weak var courseworkReminder: UILabel!
@IBOutlet weak var courseworkDueDate: UILabel!
@IBOutlet weak var viewNote: CellButton!
@IBOutlet weak var courseworkProgressBar: ProgressBar!
@IBAction func viewNotePressed(button: CellButton){
if let myCell = button.myCell, indexPath = button.myTable?.indexPathForCell(myCell) {
let entry = courseworks[indexPath.row]
print(entry.valueForKey("courseworkNotes") as! String)
}
}
Upvotes: 1
Views: 449
Reputation: 1783
ViewController.swift
1.In cellForRowAtIndexPath assign tag for button
cell.myButton.tag=indexPath.row
2.In IBAction for button,you can access that tag..
@IBAction func buttonTapped(sender: UIButton) {
let entry = courseworks[sender.tag]
//here you can implement a alertView to show popover
//according to your requirement
}
Upvotes: 1
Reputation: 129
You can set a gesture recognizer in your table, an detect if a cell was pressed, and which was.
Declare a property like this:
UILongPressGestureRecognizer * lpgr
Then:
func setUpLongPressGesture() {
self.lpgr = UILongPressGestureRecognizer(target: self, action: "handleLongPressGestures:")
self.lpgr.minimumPressDuration = 1.0
self.lpgr.allowableMovement = 100.0
self.myTable.addGestureRecognizer(self.lpgr)
}
func handleLongPressGestures(sender: UILongPressGestureRecognizer) {
if sender.isEqual(self.lpgr) {
if sender.state == .Began {
//Get the table indexPath
var p: CGPoint = sender.locationInView(self.myTable)
var indexPath: NSIndexPath = self.myTable(forRowAtPoint: p)
//if indexPath==nil means long press on table view but not on a row
if indexPath != nil {
var cell: UITableViewCell = self.myTable.cellForRowAtIndexPath(indexPath)
if cell.isHighlighted {
NSLog("long press on table view at section %ld row %ld", Int(indexPath.section), Int(indexPath.row))
self.currentIndexPath = indexPath
//Do your stuff
}
}
}
}
// End sender isEqual longPress
}
Upvotes: 0