2017-11-24 3 views
1

Ich habe eine Aktionsschaltfläche in UITableViewCell und ich möchte erkennen, wenn die Taste gedrückt wird und die Nummer der gedrückten Zelle von ViewController, um eine Audio-Play-Liste in ViewController.swift zu machen.Swift - Wie erkennt man eine Aktionstaste in UItableViewCell von ViewController?

Ich habe in diesem Problem für eine Weile stecken, und ich würde Ihren Rat wirklich schätzen. Hier sind die Codes.

ViewController.swift

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 

     tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "cell") 

    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell 
     return cell 

    } 


} 

Cell.swift

import UIKit 

class Cell: UITableViewCell { 

    @IBOutlet weak var button: UIButton! 

    @IBAction func buttonPressed(_ sender: Any) { 

     ***[Code to send the pressed cell's number to ViewController]*** 

    } 

} 
+0

Geben Sie für Delegierte. –

+0

Könnten Sie bitte genauer sein? Vielen Dank. – coonie

Antwort

2

Sie für ein gutes altmodischen Delegaten Muster gehen könnte. Dies hat den Vorteil, dass Sie Ihren View-Controller nicht von Ihrer Zelle koppeln können. Vergessen Sie nicht, Ihren Delegierten weak zu machen, um Retain-Zyklen zu vermeiden.

Sie können den Zellindexpfad in der Tabellenansicht finden. (Ich gehe davon aus durch Zellzahl Sie Indexpfad bedeuten)

protocol CellDelegate: class { 
    func didTap(_ cell: Cell) 
} 

class Cell: UITableViewCell { 

    weak var delegate: CellDelegate? 
    @IBAction func buttonPressed(_ sender: Any) { 
     delegate?.didTap(self) 
    } 
} 

class ViewController: UIViewController, CellDelegate { 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = ... 
     cell.delegate = self 
     return cell 
    } 

    func didTap(_ cell: Cell) { 
     let indexPath = self.tableView.indexPath(for: cell) 
     // do something with the index path 
    } 
} 
+2

Ihre Implementierung von 'didTap' ist seltsam. Tun Sie einfach: 'Lassen Sie indexPath = self.tableView.IndexPath (für: Zelle)'. Keine Notwendigkeit, die sichtbaren Zellen zu scannen. – rmaddy

-1

Versuchen Sie dieses innerhalb Ihrer ViewConroller

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell 

     //add tag to cell button to that of cell index 
     cell.button.tag = indexPath.row 

     //Observer for button click event inside cell 
     cell.button.addTarget(self, action: #selector(pressButton(_:)), for: .touchUpInside) 

     return cell 

    } 

//Mark: Button Action 

@objc func pressButton(_ button: UIButton) { 
    print("Button with tag: \(button.tag) clicked in cell!") 
} 
Verwandte Themen