2017-09-23 2 views
0

J'ai une vue de table que ses cellules ont un bouton en eux-mêmes et ces boutons devraient ouvrir une vue avec un identifiant unique. J'ai donc besoin de passer un argument à mes boutons mais avec la propriété addTarget je peux juste appeler la fonction sans aucun paramètre.Ajouter une cible pour le bouton dans les cellules de vue de table

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
... 
    cell.editButton.addTarget(self, action: #selector(goToEdit(id:)), for: .touchUpInside) 
} 

func goToEdit(id: String) { 
    let edit = EditAdViewController(editingAdId: id) 
    self.navigationController?.pushViewController(edit, animated: true) 
} 

Y at-il un moyen de référencer une action avec certains paramètres à un bouton? Merci à tous :)

+0

https://stackoverflow.com/a/46379494/3746301 – Shades

Répondre

0

Peut-être que vous pouvez essayer de lier votre bouton à @IBAction et utiliser params [indexPath.row].

Pour obtenir le indexPath:

var cell = sender.superview() as? UITableViewCell 
var indexPath: IndexPath? = yourTableView.indexPath(for: cell!) 
+0

malheureusement #selector ne peut obtenir aucun paramètre –

+0

essayez de vous relier à un bouton @IBAction et l'utilisation params [indexPath.row]. pour obtenir l'indexPath: var cell = sender.superview() as? UITableViewCell var indexPath: IndexPath? = yourTableView.indexPath (pour: cell!) –

+0

@LucasMoraes Ne publiez pas de code dans les commentaires. [Modifier] votre réponse sera tous les détails pertinents. – rmaddy

0

Vous pouvez essayer d'ajouter des fonctions de délégué à votre UITableViewCell personnalisé.

Par exemple, j'ai un bouton à l'intérieur de cette coutume tableViewCell:

PickupTableViewCell.swift

import UIKit 

protocol PickupTableViewCellDelegate: NSObjectProtocol { 
    func pickupTableViewCell(userDidTapPickup pickup: Pickup, pickupTableViewCell: PickupTableViewCell) 
} 

class PickupTableViewCell: UITableViewCell { 

    // MARK: - Properties 

    @IBOutlet private weak var label_UserFullName: UILabel! 
    .... 

    // MARK: - Functions 
    // MARK: IBAction 

    @IBAction func pickup(_ sender: Any) { 
     self.delegate?.pickupTableViewCell(userDidTapPickup: self.pickup, pickupTableViewCell: self) 
    } 
} 

Puis dans mon contrôleur je me conforme par le UITableViewDataSource (cellForRow) et bien sûr mettre en œuvre la fonction de délégué de mon tableViewCell.

HomeViewController.swift

// MARK: - UITableViewDataSource 

extension HomeViewController: UITableViewDataSource { 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let pickupTVC = tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.pickupTableViewCell)! 
     pickupTVC.delegate = self 
     pickupTVC.pickup = self.pickups[indexPath.section] 

     return pickupTVC 
    } 
} 

// MARK: - PickupTableViewCellDelegate 

extension HomeViewController: PickupTableViewCellDelegate { 
    func pickupTableViewCell(userDidTapPickup pickup: Pickup, pickupTableViewCell: PickupTableViewCell) { 
     // Do something 
    } 
}