2017-03-03 1 views
0

Ich habe eine Tabellenansicht in einer VC, die über einen Navigationscontroller verfügt und benutzerdefinierte Tabellenzellen enthält. Ich habe mich gefragt, was die beste Vorgehensweise ist, um auf den Navigations-Stack des übergeordneten VCs zu gelangen, wenn eine Schaltfläche in der benutzerdefinierten Tabellenzelle angetippt wird. Ich bin in der Lage, dies zu funktionieren, wenn ich den Navigationscontroller der Eltern-VC an die Zelle übergebe; Aber ist das die effektivste/effizienteste Praxis? Bitte beachten Sie meine aktuelle Implementierung unter:iOS Swift: Verschieben einer Ansicht auf den Stack aus einer benutzerdefinierten Tabellenansicht Zelle

UserAccountVC:

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

     let cell:TextPostTableViewCell = Bundle.main.loadNibNamed("TextPostTableViewCell", owner: self, options: nil)?.first as! TextPostTableViewCell 

     cell.setupCell(navigationController: self.navigationController!) 

     cell.selectionStyle = .none 

     return cell 
} 

CustomTableCell:

import UIKit 

class TextPostTableViewCell: UITableViewCell { 

    var aNavigationController: UINavigationController! 

    //MARK: Actions 
    @IBAction func profilePicButtonTapped() { //We want to present a users profile 

     let sb = UIStoryboard(name: "SuccessfulLogin", bundle: nil) 
     let cc = (sb.instantiateViewController(withIdentifier: "otherUserViewController")) as! OtherUserAccountViewController 
     self.aNavigationController.pushViewController(cc, animated: true) 
    } 

    func setupCell(navigationController: UINavigationController) -> Void { 

     aNavigationController = navigationController 
    } 
} 

Vielen Dank im Voraus!

Antwort

1

Nein, das ist keine bewährte Methode. Sie können einen IBAction im Interface Builder für Ihre UIButton einrichten oder Ihre UIViewController als Ziel in cellForRowAt hinzufügen. Bei beiden Methoden können Sie einige Verfahren benötigen die indexPath identifizieren, da Sie nicht didSelectRow in Ihrem Tableview Delegat verwenden:

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

    let cell:TextPostTableViewCell = Bundle.main.loadNibNamed("TextPostTableViewCell", owner: self, options: nil)?.first as! TextPostTableViewCell 
    cell.button.tag = indexPath.row // Or use some other method of identifying your data in `myAction(_:)` 
    cell.button.addTarget(self, action:, @selector(myAction(_:)), for: .touchUpInside) 
    ... 
} 
+0

Vielen Dank, das ist gut zu wissen! –

1

Sie können mit Delegierten in dieser Situation.
Der Code ist ein bisschen mehr hier, aber das ist der bessere Weg in iOS-Entwicklung IMO.

class ViewController: UIViewController { 

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

     let cell:TextPostTableViewCell = Bundle.main.loadNibNamed("TextPostTableViewCell", owner: self, options: nil)?.first as!  TextPostTableViewCell 

     cell.delegate = self 

     cell.selectionStyle = .none 

     return cell 
    } 
} 

extension ViewController: TextPostTableViewCellDelegate { 
    func didTappedProfilePicButton() { 
     let sb = UIStoryboard(name: "SuccessfulLogin", bundle: nil) 
     let cc = (sb.instantiateViewController(withIdentifier: "otherUserViewController")) as! OtherUserAccountViewController 
     navigationController?.pushViewController(cc, animated: true) 
    } 
} 

protocol TextPostTableViewCellDelegate: class { 
    func didTappedProfilePicButton() 
} 

class TextPostTableViewCell: UITableViewCell { 

    weak var delegate: TextPostTableViewCellDelegate? 

    //MARK: Actions 
    @IBAction func profilePicButtonTapped() { //We want to present a users profile 
     delegate?.didTappedProfilePicButton() 
    } 

} 
+0

Vielen Dank! –

Verwandte Themen