質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

3541閲覧

UICollectionViewのセル内にテーブルビューを表示し、カテゴリーに沿ってスワイプでビューを切り替えたいです。

obataatu

総合スコア14

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/06/23 15:53

お世話になります。
現在、swiftを勉強中の初心者です。
UICollectionViewに関するご質問です。
UICollectionViewのセル内にテーブルビューを表示し、カテゴリーに沿ってスワイプでビューを切り替えたいと思っております。
現在、単純なViewのみをスクロールで切り替えることはできているのですが、そこにテーブルビューを入れ込む方法が分からなく、困っております。
知識が少なく、そのようなことが可能であるのかもわからない状況ですが、お分かりになる方がいらっしゃれば教えて頂けますと幸いです。
現在のコードを下記に記載致します。よろしくお願いいたします。

import UIKit struct Pages2 { var viewControllers:[UIView] = [] } class ViewController: UIViewController { @IBOutlet weak var collect1: UICollectionView! var pages2:Pages2 = Pages2(){ didSet { self.collect1?.reloadData() } } override func viewDidLoad() { super.viewDidLoad() let wall = UIView() wall.backgroundColor = UIColor.blueColor() let width:CGFloat = self.view.bounds.width let heigth:CGFloat = self.view.bounds.height wall.frame = CGRectMake(0, 0, width, heigth) self.pages2.viewControllers.append(wall) let wall2 = UIView() wall2.backgroundColor = UIColor.greenColor() let width2:CGFloat = self.view.bounds.width let heigth2:CGFloat = self.view.bounds.height wall2.frame = CGRectMake(0, 0, width2, heigth2) self.pages2.viewControllers.append(wall2) let wall3 = UIView() wall3.backgroundColor = UIColor.redColor() let width3:CGFloat = self.view.bounds.width let heigth3:CGFloat = self.view.bounds.height wall3.frame = CGRectMake(0, 0, width3, heigth3) self.pages2.viewControllers.append(wall3) self.collect1.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.pages2.viewControllers.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell",forIndexPath: indexPath) as UICollectionViewCell //viewの背景を指定している cell.backgroundColor = UIColor.whiteColor() let view = self.pages2.viewControllers[indexPath.row] cell.contentView.addSubview(view) return cell } /// セルの大きさ func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let pageViewRect = self.collect1.bounds return CGSize(width: pageViewRect.width, height: pageViewRect.height) } /// 横のスペース func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { return 0 } /// 縦のスペース func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { return 0 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

これは、横スクロール(横スワイプ)でUICollectionView内に配置したテーブルビューを切り替え、
縦スクロール(縦スワイプ)で、テーブルビュー内の行をスクロールアップ/ダウンさせたいということでしょうか?

もしそうでしたら、とりあえず
Collection View Flow Layoutの設定をHorizontalにし、
以下のようなCustomTableViewを作成し、

swift

1import UIKit 2 3class CustomTableView: UITableView, UITableViewDelegate, UITableViewDataSource { 4 5 init() { 6 super.init(frame: CGRectZero, style: .Plain) 7 self.delegate = self 8 self.dataSource = self 9 self.registerClass(UITableViewCell.self, forCellReuseIdentifier: "TableCell") 10 } 11 12 required init?(coder aDecoder: NSCoder) { 13 fatalError("init(coder:) has not been implemented") 14 } 15 16 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 return 100 18 } 19 20 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 21 let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) 22 cell.backgroundColor = tableView.backgroundColor 23 cell.textLabel?.text = "row no=\(indexPath.row)" 24 return cell 25 } 26 27}

let wall = UIView()
let wall2 = UIView()
let wall3 = UIView()
をそれぞれ
let wall = CustomTableView()
let wall2 = CustomTableView()
let wall3 = CustomTableView()
に変えるだけでできました。
collect1.pagingEnabled=true
も加えておいた方がたぶん使いやすいと思います。

投稿2016/06/24 09:54

TakeOne

総合スコア6299

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

obataatu

2016/06/24 10:14

ご返信頂き、ありがとうございます。 頂いたサンプルとご指摘された点を修正することで理想的な動きになりました。 CustomTableVieのサンプルの方は、勉強不足で頭の中にない考え方でしたので、しっかり理解し、今後の開発に活かしたいと思います。 毎回、的確なアドバイスを頂き、大変お世話になっております。 ありがとうございます!
obataatu

2016/07/01 07:11

連続のご質問の失礼致します。 初歩的な質問でしたら、申し訳ありません。 6/24に頂いたカスタムテーブルビューのサンプルについてですが、didSelectRowAtIndexPathにてセルをタップした際に押されるセルによって違う場所に遷移したいのですが、他のビューコントローラーに埋め込んで使う場合、画面遷移ができません。 現在、セルのindexPath.rowの数値によって、ビューコントローラー側でnavigationController?.pushViewControllerを使っているのですが、その他の方法があるのでしょうか? お忙しいところ大変恐れ入りますが、ご回答頂けますと、幸いです。 よろしくお願いします。
TakeOne

2016/07/01 09:03 編集

単に「できません」と言われてもエラーが出るのか何も反応しないのかわからないし、ビューコントローラー側でnavigationController?.pushViewControllerを使っていると言われても、どうやってビューコントローラーにイベントを伝えてそれを使っているのかとか何もわかりません。 現状のコーディングとかどんなエラーが発生するのかとかちゃんと情報を開示して質問した方がよいと思います。
obataatu

2016/07/01 11:27

ご返信ありがとうございます。 大変失礼致しました。 下記に現状のコードを記載致します。 目的としましては、ViewController2に表示したテーブルビュー(前画面の動作次第で異なるテーブルビューが表示される予定です)からindexPath.rowが0のセルが押された場合にViewController3に飛びたいと思っております。 しかし、セルを押した段階でEXC_BAD_INSTRUCTIONというエラーが出て止まってしまいます。 セルが押された際にindexPath.rowを表示させてみた結果反応はしているので、移動部分のコードに問題があるのでしょうか? わかりずらい説明となってしまい、申し訳ありませんが、ご教授頂けますと幸いです。 ```swift ViewController2 import UIKit class ViewController2: UIViewController { var box2:UIView! @IBOutlet weak var top_view: UIView! override func viewDidLoad() { super.viewDidLoad() self.box2 = CustomTableView() self.box2.frame = CGRectMake(0, 0, self.view.bounds.width,self.view.bounds.height-120) top_view.addSubview(self.box2) } func to_greenpage(){ let to_greenpage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3 self.presentViewController(to_greenpage, animated: true, completion: nil) } func to_bluepage(){ let to_bluepage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController4") as! ViewController4 navigationController?.pushViewController(to_bluepage, animated: true) } func to_redpage(){ let to_redpage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController5") as! ViewController5 navigationController?.pushViewController(to_redpage, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } ``` ```swift カスタムテーブルビュー swift import UIKit class CustomTableView: UITableView ,UITableViewDelegate, UITableViewDataSource { var memos:[String] = ["店員1","店員2","店員3","店員4","店長さん1"] var gazo:[UIImage]? = [UIImage(named:"店員1")!,UIImage(named:"店員2")!,UIImage(named:"店員3")!,UIImage(named:"店員4")!,UIImage(named:"店長さん1")!] init() { super.init(frame: CGRectZero, style: .Plain) self.delegate = self self.dataSource = self self.rowHeight = 90 self.registerClass(UITableViewCell.self, forCellReuseIdentifier: "TableCell") } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return memos .count } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print(indexPath.row) if (indexPath.row == 0) { ViewController2().to_greenpage() } else if(indexPath.row == 1){ ViewController2().to_bluepage() } else if(indexPath.row == 2){ ViewController2().to_redpage() } else { return } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) cell.backgroundColor = tableView.backgroundColor cell.textLabel?.text = memos[indexPath.row]; cell.textLabel?.font = UIFont.boldSystemFontOfSize(25) cell.textLabel?.textColor = UIColor.blackColor() let gazo1 = gazo![indexPath.row] cell.imageView?.image = gazo1.resize3(CGSizeMake(100, 150)) cell.imageView?.contentMode = .ScaleAspectFill return cell } } extension UIImage { func resize3(size: CGSize) -> UIImage { let widthRatio = size.width / self.size.width let heightRatio = size.height / self.size.height let ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio let resizedSize = CGSize(width: (self.size.width * ratio), height: (self.size.height * ratio)) // 画質を落とさないように以下を修正 UIGraphicsBeginImageContextWithOptions(resizedSize, false, 0.0) drawInRect(CGRect(x: 0, y: 0, width: resizedSize.width, height: resizedSize.height)) let resizedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return resizedImage } } ```
TakeOne

2016/07/01 17:00 編集

ViewController2().to_redpage() とやると、現在表示中のViewController2ではなく、新しいViewController2を生成して、その新しいViewController2のto_redpageを呼び出すことになります。 新しいViewController2は、Storyboardで生成したものではありませんから、to_redpageに書かれている「self.storyboard?」はnilです。このため新しいViewController5が正しく生成できずにエラーになるのだと思います。 CustomTableViewが現在表示中のViewControllerにイベントを通知したかったら、前回の質問でサンプルを示したようにdelegateを作って通知すればよいと思います。
obataatu

2016/07/01 18:10

ご返信頂き、ありがとうございます。 自分だけではエラーの原因のイメージができませんでしたので、わかりやすくご説明頂き、大変助かりました。 ご指摘いただいた通り、デリゲートを作り、メソッドを渡すことにします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問