2017-03-03 6 views
0

ナビゲーションコントローラを持つVC内にテーブルビューがあり、カスタムテーブルセルが含まれています。カスタムテーブルセルのボタンがタップされている場合、親VCのナビゲーションスタックにプッシュするのがベストプラクティスであると思っていました。親VCのナビゲーションコントローラをセルに渡すと、これを動作させることができます。これは最も効果的な/効率的な練習ですか?iOS Swift:カスタムTableviewセル内からビューをスタックにプッシュする

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 
    } 
} 

事前にありがとうございます!

答えて

1

いいえ、これはベストプラクティスではありません。 UIButtonのインタフェースビルダーにIBActionを設定するか、またはcellForRowAtUIViewControllerを追加して追加することができます。

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

ありがとう、これは知っておいてよかったです! –

1

あなたはこのような状況では、デリゲートを使用することができます:あなたがテーブルビューのデリゲートにdidSelectRowを使用していないので、どちらかの方法を使用すると、indexPathを識別するいくつかの方法が必要な場合があります。
コードはもう少しここにありますが、これはiOS開発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

ありがとうございます! –

関連する問題