使用UIButton Action在UITableViewCell中显示一个popover

时间:2016-05-15 01:40:14

标签: ios swift uitableview uibutton

我有一个包含UIButton的自定义UITableViewCell。当我按下按钮时,我想显示带有一些文字的弹出窗口。文本将根据按下的indexPath而有所不同。

到目前为止,

是我的代码。

class CellButton: UIButton {
 weak var myTable: UITableView?
 weak var myCell:  UITableViewCell?
 }

这是我的自定义UITableViewCell。我有一个打印一行的按钮动作,我希望将其显示为一个弹出窗口。

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)




}
}

2 个答案:

答案 0 :(得分:1)

<强> ViewController.swift

1.在cellForRowAtIndexPath中为按钮分配标签

  @IBAction func buttonTapped(sender: UIButton) {

     let entry = courseworks[sender.tag]
     //here you can implement a alertView to show popover 
            //according to your requirement
  }

2.在IBAction for按钮中,您可以访问该标签..

exists

答案 1 :(得分:0)

您可以在表格中设置手势识别器,检测是否按下了单元格,以及哪个是。

声明一个这样的属性:

UILongPressGestureRecognizer * lpgr

然后:

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
}