2017-09-11 11 views
0

のストーリーボードをインスタンス化している間にデリゲートを使用してデータを渡す問題他のユーザーからの同様の問題を扱っていたと思われる様々な投稿を行ってしまったが、率直に言って、 。:)私の問題は、このようなものになります..識別子が

私は、いくつかのtableviewセルがロードされているtableviewを持っています。これらのセルで特定の編集アイコンをクリックすると、別のviewcontrollerに移動します。また、テーブルビューのセルにはラベルであるproductNameがあります。

編集ボタンをクリックしたときに問題が発生しました。これをナビゲーションバーの[戻る]ボタンに表示できるように、このproductNameを別のビューコントローラに移動します。 tableviewcellクラスでは、私は、このtableviewcellがロードされるtableviewcontrollerクラスでそう...

@IBAction func editBtnTapped(_ sender: Any) { 
     if let _ = delegate { 
      delegate?.editBtnTapped(cell: self) 
     } 
    } 

のように、そう...

protocol MyCellDelegate { 
    func editBtnTapped(cell: ProductsTableViewCell) 
} 

し、[編集]ボタンをタップ上のようにデリゲートをしました私はそう...

func editBtnTapped(cell: ProductsTableViewCell) { 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let controller = storyboard.instantiateViewController(withIdentifier: "editProductsIdentifier") 
    let navController = UINavigationController(rootViewController: controller) 
    self.present(navController, animated: true, completion: nil) 

} 

は、ここで私は別のViewControllerを提示していますし、それは私がに合格する必要があり、こののViewControllerにあるようなeditBtnTapped法と呼ばれています私は自分の tableviewcellクラスにあるです。そして、私は達成することはできませんよ、この.. :(

+0

問題点を教えてください。 –

+0

私は自分のtableviewcellに持っているラベルを...私が識別子をロードしたviewcontrollerに渡したいと思っています... –

+0

@ Usman Javedまた、私は上記に示していないラベルです。しかし、私はそれを私のtableviewcellクラスと宣言しました... –

答えて

1

目的地のViewController(idで1:editProductsIdentifier)でvar titleFromCell = ""のような変数を追加:によって

let controller = storyboard.instantiateViewController(withIdentifier: "editProductsIdentifier") 

は、この行を置き換えますこれらの2行:

let controller = storyboard.instantiateViewController(withIdentifier: "editProductsIdentifier") as! //The Custom Class of this View controller 
controller.titleFromCell = cell.productName.text 

が次にあなたがナビゲーションバーに表示することができます

1

tableSelectedRowIndexをセルクラス参照を渡す代わりにfunc editBtnTapped()に渡すことができます。これにより、簡単に目標を達成することができます。またtableViewCellクラスで

@IBOutlet weak var btnEdit: UIButton! 

を編集ボタンのIBOutletを作成し、

@IBAction func editBtnTapped(_ sender: UIButton) { 
     if let _ = delegate { 
      delegate?.editBtnTapped(SelectedIndex: sender.tag) 
     } 
    } 
そのアクションを作成するには、この

protocol MyCellDelegate { 
    func editBtnTapped(SelectedIndex : Int) 
} 

ようtableViewCellクラスでこの

宣言プロトコルと同様に行うことができます

次に、indexPath.rowのeditbttonタグ値をに設定します方法

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

    let cellIdentifier = "customCell" 
    var cell : CustomCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CustomCell? 
    if (cell == nil) { 

     cell = Bundle.main.loadNibNamed("CustomCell", owner: nil, options: nil)?[0] as? CustomCell 
    } 

    cell.btnEdit.tag = indexPath.row 
    cell.delegate = self 
    return cell! 
} 

    func editBtnTapped(SelectedIndex : Int) { 

     let productName = // get name of product like you display in cellForRowAtIndexPath 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let controller = storyboard.instantiateViewController(withIdentifier: "editProductsIdentifier") 
     controller.productName = // Pass your product name 
     let navController = UINavigationController(rootViewController: controller) 
     self.present(navController, animated: true, completion: nil) 

    } 

難しいものが見つかった場合は教えてください。

+0

解決に感謝します。 upvoteを与えている... :)それは最初に与えられてから働いたので、上記の答えを受け入れた... –