2017-03-03 26 views
0

我在具有導航控制器並且包含自定義表格單元格的VC中有一個tableview。我想知道如果點擊自定義表格單元格中的按鈕,推入父VC的導航堆棧的最佳做法是什麼。如果我將父VC的導航控制器傳遞給單元格,我可以得到這個工作;但這是最有效/最有效的做法嗎?請參閱我目前的執行如下:iOS Swift:從自定義表格視圖中將視圖推送到堆棧單元格

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,或者在cellForRowAt中添加您的UIViewController作爲目標。無論使用哪種方法,你可能需要識別indexPath的一些方法,因爲你是不是在你的tableview委託使用didSelectRow

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

您可以在此情況下使用的委託。
這裏的代碼稍多一些,但這是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

非常感謝! –

相關問題