テクノロジー

swift ~ 任意のUITableViewCell&UICollectionViewCellからCell情報を持って異なるStoryboardに画面遷移 ~

Daigo Ichikawa 's icon'
  • Daigo Ichikawa
  • 2020/03/03 13:15

JapanFuse CTOの市川です。

Content image

 

ColleColleを開発していて、「情報を保持して異なるstoryboardに遷移する方法」でよりいい方法を見つけたので、久々に記事を書いてみようと思います。

しかも最近コードブロック機能ができたと聞いたので.....

備忘録兼、もし同じことで困っている方がいらっしゃたらその一助になればと思います。

前提

UITableViewCellやUICollectionViewCellに構造体を表示していて、
そしてその構造体のプロパティを持って別の画面に遷移したい!
使うメソッドは以下の二つです!

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

そして扱う構造体は以下の通りです。

struct User {

   var name: String!
   var money: Int!

   init(name: String, money: Int) {

   self.name = name
   self.money = money

  }
}

UITableViewやUICollectionView

をそれぞれ

tableView, collectionView

として、Cell に表示する構造体が格納されている配列を

list: [User]!

とします!


そして、(前置きが長くてすみません💦)
遷移先のStoryboardの設定ですが、

「xxx.storyboard」にViewControllerを置いて、ViewControllerのswiftFileを作るのを忘れないでください!

そして一番忘れないで欲しいのが、ViewcontrollerにNavigationControllerを付けて、このNavigationControllerに「is Initial View Controller」にチェックを付けて、これに「StoryboradID」をつけるのを忘れないでください!

 

ここではtableViewからの遷移先を
Next1.storyboard の Next1ViewController で制御するViewControllerとそのNavigationControllerの「StoryboradID」を navigation1 とし

collectionViewからの遷移先を
Next2.storyboard の Next2ViewController で制御するViewControllerとそのNavigationControllerの「StoryboradID」を navigation2 とします!

 

そして、Next1ViewControllerとNext2ViewControllerには

class Next1ViewController {
   var argString1: String!
   var argInt1: Int!
}

class Next2ViewController {
   var argString2: String!
   var argInt2: Int!
}

と変数が置かれていることにします!
前置きが長くなりました💦それでは行ってみましょう!

 

またここから、tableViewとcollectionViewが同じファイル内にあるが如く(見やすくするため)swift文を書いていきます。(実際に書くときはご自分の裁量でお願いします!)

var list: [User]!

var tableView: UITableView!
var collectionView: UICollectionView!

//いろいろ省略します。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let x = indexPath.row
        let y = list[x]
        let storyboard: UIStoryboard = UIStoryboard(name: "Next1", bundle: nil)
        let navigationController = storyboard.instantiateViewController(withIdentifier: "navigation1") as! UINavigationController
        let vc = navigationController.topViewController as! Next1ViewController
         vc.argString1 = y.name
         vc.argInt1 = y.money
        self.present(navigationController, animated: true, completion: nil)
    }

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let x = self.tableView.indexPathForSelectedRow
        let y = x?.row
        let z = list[y!]
        let storyboard: UIStoryboard = UIStoryboard(name: "Next2", bundle: nil)
        let navigationController = storyboard.instantiateViewController(withIdentifier: "navigation2") as! UINavigationController
        let vc = navigationController.topViewController as! Next2ViewController
         vc.argString2 = z.name
         vc.argInt2 = z.money
        self.present(navigationController, animated: true, completion: nil)
}

そして、余談なのですが遷移させた情報を、Next1ViewController、Next2ViewControllerのUIで表示したい場合は別の変数に入れ直さないとダメらしいです、、、

つまり、遷移させた情報をそのままUIで表示できないということ。

なので私は、

class Next1ViewController {

   var argString1: String!
   var argInt1: Int!

   @IBOutlet weak var label: UILabel!

   var DamyInt1: Int!

   override func viewDidLoad() {
        super.viewDidLoad()
       self.DamyInt1 = argInt1
       self.label.text = DamyInt1
    }

}

というように他の変数を間に入れてUIを初期化しています。
もし、間違い等があればご指摘お願いいたします。

Supporter profile iconSupporter profile iconSupporter profile icon
Article tip 3人がサポートしています
獲得ALIS: Article like 25.15 ALIS Article tip 91.00 ALIS
Daigo Ichikawa 's icon'
  • Daigo Ichikawa
  • @NUlized30Y
CTO of JapanFuse,Inc.

投稿者の人気記事
コメントする
コメントする
こちらもおすすめ!
Eye catch
クリプト

ブロックチェーンの51%攻撃ってなに

Like token Tip token
0.00 ALIS
Eye catch
クリプト

約2年間ブロックチェ-ンゲームをして

Like token Tip token
61.20 ALIS
Eye catch
クリプト

スーパーコンピュータ「京」でマイニングしたら

Like token Tip token
1.06k ALIS
Eye catch
テクノロジー

iOS15 配信開始!!

Like token Tip token
7.20 ALIS
Eye catch
テクノロジー

オープンソースプロジェクトに参加して自己肯定感を高める

Like token Tip token
85.05 ALIS
Eye catch
クリプト

NFT解体新書・デジタルデータをNFTで販売するときのすべて【実証実験・共有レポート】

Like token Tip token
120.79 ALIS
Eye catch
ゲーム

ドラクエで学ぶオーバフロー

Like token Tip token
30.10 ALIS
Eye catch
他カテゴリ

機械学習を体験してみよう!(難易度低)

Like token Tip token
124.82 ALIS
Eye catch
クリプト

ジョークコインとして出発したDogecoin(ドージコイン)の誕生から現在まで。注目される非証券性🐶

Like token Tip token
38.31 ALIS
Eye catch
クリプト

Bitcoinの価値の源泉は、PoWによる電気代ではなくて"競争原理"だった。

Like token Tip token
159.32 ALIS
Eye catch
クリプト

17万円のPCでTwitterやってるのはもったいないのでETHマイニングを始めた話

Like token Tip token
46.60 ALIS
Eye catch
クリプト

Bitcoin史 〜0.00076ドルから6万ドルへの歩み〜

Like token Tip token
947.13 ALIS