1

Preciso que quando o usuário pressionar a célula da tabela apareça um alerta com informações.

Segue código

import UIKit

class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "Bem da Água"
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let linha = indexPath.row
        println("Selecionou \(linha)")

        let Gest = UILongPressGestureRecognizer(target: self, action: "showAlerta:")
        self.view.addGestureRecognizer(Gest)
    }

    func showAlerta(sender: UILongPressGestureRecognizer){
        if sender.state == UIGestureRecognizerState.Began {
            var alerta = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)

            let show = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler:nil)

            alerta.addAction(show)

            presentViewController(alerta,animated:true,completion:nil)
        }
    }
}

1 Resposta 1

1

É preciso adicionar o gesto na própria célula, dentro do cellForRowAtIndexPath, por exemplo:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! UITableViewCell

    let gesture = UILongPressGestureRecognizer(target: self, action: "showAlerta:")
    cell.addGestureRecognizer(gesture)

    return cell
}

E para obter o index path da célula pressionada, no seu método showAlerta faça:

func showAlerta(sender: UILongPressGestureRecognizer) {
    var point: CGPoint = sender.locationInView(tableView)
    var indexPath: NSIndexPath = tableView.indexPathForRowAtPoint(point)!
    // ...
}
5
  • Aqui me retorna um erro cara no identifier cell
    – Faro
    15/07/2015 às 23:06
  • Não sei como está esse seu método, que é como você monta a sua célula, então fiz uma suposição para definir a variável cell. 15/07/2015 às 23:10
  • Exatamente como você colocou
    – Faro
    15/07/2015 às 23:18
  • Pode especificar melhor o erro? Não consegui identificar onde exatamente está o erro que você citou. 16/07/2015 às 0:50
  • Ignore esse erro, pois foi um descuido que tive e foi resolvido.
    – Faro
    16/07/2015 às 15:22

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .