点击识别器仅在第一个单元格中工作

时间:2016-02-05 20:45:04

标签: ios swift uitableview

我有一个带有一个原型单元格的UITableView,我添加了一个长按手势识别器,它在代码中有这样的动作:

var contatos = db.collection('contatos');
contatos.findOne(...);

但是当我运行该项目时,它只适用于表格的第一个单元格。我怎样才能使它适用于所有这些?

2 个答案:

答案 0 :(得分:1)

检查控制台,确定您会看到iOS 9及更高版本的错误。将手势识别器从单元格移动到表格视图并说:

@IBAction func Itinerario(sender: UITapGestureRecognizer) {
    //if you need the cell or index path 
    let location = sender.locationInView(self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(location)
    let cell = self.tableView.cellForRowAtIndexPath(indexPath)
    if indexPath != nil
    {
      let a = self.tabla
      let b: String = "\n"
      let c: String = "\n"
      let d: String = "*Lugar Destacado"
      let sum = a + b + c + d

      let alert = UIAlertController(title: "Itinerario de Procesión", message: "\(sum)", preferredStyle: UIAlertControllerStyle.Alert)
      let action1 = UIAlertAction(title: "Atrás", style: UIAlertActionStyle.Cancel, handler: nil)

      alert.addAction(action1)


      self.presentViewController(alert, animated: true, completion: nil);
    }


}

答案 1 :(得分:0)

这是我的应用程序的解决方案!现在它有效!!! :) 这么多@beyowulf !!!

        // MARK: - Long Press Gesture Action Sheet
    @IBAction func popUpActionCell(longPressGesture : UILongPressGestureRecognizer)
    {
        // Delete selected Cell
        let point = longPressGesture.location(in: self.collectionView)
        let indexPath = self.collectionView?.indexPathForItem(at: point)
//        let cell = self.collectionView?.cellForItem(at: indexPath!)
        if indexPath != nil
        {
            let alertActionCell = UIAlertController(title: "Action Recipe Cell", message: "Choose an action for the selected recipe", preferredStyle: .actionSheet)

            // Configure Remove Item Action
            let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { action in
                RecipeDataManager.shared.recipes.remove(at: indexPath!.row)
                print("Cell Removed")
                self.collectionView!.reloadData()
            })

            // Configure Cancel Action Sheet
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { acion in
                print("Cancel actionsheet")
            })

            alertActionCell.addAction(deleteAction)
            alertActionCell.addAction(cancelAction)
            self.present(alertActionCell, animated: true, completion: nil)

        }
    }