2016-10-30 15 views
0

多くの方法を試しましたが、まだ別のViewControllerに行くことはできません。これは私のストーリーボードです。 imgStoryboardViewControllerへのTableViewCell - Swift

これは私のコードです。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryMainTableViewCell { 
     listItems.sort() { $0.seq > $1.seq } 
     let history = listItems[indexPath.row] 
     cell.setValue(history: history) 

     return cell 
    } else { 
     return HistoryMainTableViewCell() 
    } 
} 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "vcProductHistory" { 
     if let productHistoryPage = segue.destination as? HistoryProductViewController { 
      if let IndexPath = tableViewHistory.indexPathForSelectedRow { 
       let historyArray = listItems[IndexPath.row] 
       productHistoryPage.history = historyArray 
      } 
     } 
    } 
} 
+0

Segueを 'HistoryMainTableViewCell'から' HistoryProductViewController'に追加しましたか? – lee5783

+0

VCがセグを開始することを知らせるためにイベントが発生する場所でperformSegue関数を追加していますか? – JustinM

+0

@ lee5783すでに追加しました –

答えて

0

このコードを試してみてください:(未テストコード)を

注:コードの下はセグエを準備トリガされます。とき、tableViewCellは私の最初のコメントにあなたの答えに基づいて...私はコードが動作する知っている

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { 

    // let indexPath = myTableView!.indexPathForSelectedRow 
    // let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell 
    //sendSelectedData = (currentCell.textLabel?.text)! as String as NSString 
     performSegue(withIdentifier: "vcProductHistory", sender: self) 
} 
+0

ありがとうございますがまだ動作していません –

0

をselected.Let:先のVCに最初のVCからのラインをドラッグすると、セグエ伝えるための準備、セグエについてのあなたのVCを伝えますあなたのVCは、セグがトリガされたとき何をするのですか?あなたは、セグを実行すべき時をあなたのVCに知らせる必要があります。これを試してみてください:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryMainTableViewCell else { return HistoryMainTableViewCell() } 

    let history = listItems[indexPath.row] 
    cell.setValue(history: history) 

    return cell 
    } 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "vcProductHistory" { 
     guard let productHistoryPage = segue.destination as? HistoryProductViewController, let indexPath = sender as? IndexPath else { return } 

     let historyArray = listItems[indexPath.row] 
     productHistoryPage.history = historyArray 
      } 
     } 

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { 

     performSegue(withIdentifier: "vcProductHistory", sender: indexPath) 
} 

ガードシンタックスを使用して少し綺麗です。また、tableView.indexPathForSelectedRowを使用せずに、送信者をindexPathとして使用します。 cellForRowAtIndexPathで

listItems.sort() { $0.seq > $1.seq } 

あなたの並べ替えデータソースを:

また、私はあなたがcellForRowAtIndexPathでこれを持って気づきました。その関数は各行ごとに個別に呼び出されるため、そこでは実行できません。あなたはviewWillAppearのようなどこかで、それが呼び出される前にソートを行う必要があります。また、配列内の配列をソートするには、ソート後に()を削除する必要があります。これを試して。

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    listItems.sort { $0.0.seq > $0.1.seq } 
    tableView.reloadData() 
} 
+0

まだ動作していません –

+0

エラーが発生していますか? didSelectRowAtIndexPathでprintステートメントを使用しようとしましたか?すべてがテーブルに正しく表示されていますか?行を選択すると、選択されたことを示す色合いの変化が表示されますか?いくつかのprintステートメントを使用して何が呼び出されていないかを把握してください。すべてが間違っているか、間違った識別子を使用している必要があります。 – JustinM

+0

エラーが表示されません。私はdidSelectRowAtIndexPathのステートメントを印刷しますが、何も表示されませんが、行を選択すると色合いが変わります –

0

は、私はあなたがあなたのMainViewControllerのセグエは、ナビゲーションコントローラに接続されているためではない

が、私はそれがなく、作品prepareForSeguesegue.identifier == "vcProductHistory" FUNCに推測

をHistoryProductionViewControllerし、UINavigationControllerに接続すべきだと思う、if let productHistoryPage = segue.destination as? HistoryProductViewControllerはない

の作品

目的のViewControllerのクラスがNavigationControllerであるため

あなたはあなたのソリューション

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "vcProductHistory" { 
     if let naviVC = segue.destination as? UINavigationController { 
      if let productHistoryPage = naviVC.topViewController as? HistoryProductViewController { 
       if let IndexPath = tableViewHistory.indexPathForSelectedRow { 
        let historyArray = listItems[IndexPath.row] 
        productHistoryPage.history = historyArray 
       } 
      } 
     } 
    } 
} 
+0

私はそれを変更しましたが、まだ動作していません –

0

を呼び出す必要があります - あなたはそれを説明するように - 動作するはずです。 ここでの作業の多くは、ストーリーボードで直接行うことができます。 TableViewのプロトタイプセルを選択し、SegueをNavigationControllerにドラッグしてpresentModallyを選択します。
Variant 1 Setup

これ以上の魔法を使わなくても動作します。 TableViewCellを選択する場合は、NavigationControllerを提示する必要があります。この時点でコードは必要ありません(TableViewの人口を除く)。
はない場合 - あなたは多分No Selectionに選択を設定することで、あなたのTableViewを誤設定:

Selection set to no selection?

またはあなたがどこかfalseにUser Interaction Enabledを設定します。

+0

私はそれをまだ設定していますが、まだ動作しません。 –

+0

@PanPipatpunlopこれは変です。 3つのViewControllerを含むサンプルプロジェクトを提供できますか? ( "Main"と "Target ViewController"でマークされています) –

関連する問題