2017-12-02 25 views
1

私はcollectionView内にtableViewを配置し、デリゲートを追加しました。問題は、collectionView内の異なるtableViewsのデータを制御したいということです。UICollectionViewのUITableView

すべてのcollectionViewには、現在の日の名前(「Monday」など)のラベルが保持されます。ラベルの下にはtableViewがあります。テーブルビューでは、collectionViewのインデックスに基づいて異なるデータを表示する必要があります。

私の例では、すべてのtableViewには2つのセルがあります。 collectionViewの最初の項目のtableViewには、cell1: "1"とcell2: "2"と表示され、2番目の項目のtableViewにcell1: "3"とcell2: "4"と表示されます。

import UIKit 

class StundenplanCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     myHours.append(myH0) 
     myHours.append(myH1) 
     myHours.append(myH2) 
     myHours.append(myH3) 
     myHours.append(myH4) 
     myHours.append(myH5) 

     collectionView.delegate = self 
     collectionView.dataSource = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    //MARK: Outlets 
    @IBOutlet weak var collectionView: UICollectionView! 

    //MARK: Variables 
    var myDays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"] 
    var myHours: [[String]] = Array() //Array that holds myH1 - myH5 
    var myH0 = ["1", "2"] 
    var myH1 = ["3", "4"] 
    var myH2 = ["5", "6"] 
    var myH3 = ["7", "8"] 
    var myH4 = ["9", "10"] 
    var myH5 = ["Error", "Error"] 

    var tableLoop = 0 

    var headerColor: UIColor = UIColor(red: CGFloat(255/255.0), green: CGFloat(140/255.0), blue: CGFloat(60/255.0), alpha: CGFloat(1.0)) 


    //MARK: Table and Collection View 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return myDays.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell: StundenplanCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "stundenplanCollectionCell", for: indexPath) as! StundenplanCollectionViewCell 
     cell.titleLabel.text = myDays[indexPath.row] 
     cell.titleLabel.backgroundColor = headerColor 
     cell.titleLabel.textColor = UIColor.white 
     return cell 
    } 


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return myHours[tableLoop].count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: StundenplanTableViewCell = tableView.dequeueReusableCell(withIdentifier: "stundenplanTableCell", for: indexPath) as! StundenplanTableViewCell 
     cell.titleLabel.text = myHours[/*Here should the indexPath.row of the collectionView item be placed*/][indexPath.row] 
     return cell 
    } 
} 

class StundenplanCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var tableView: UITableView! 
} 

class StundenplanTableViewCell: UITableViewCell { 
    @IBOutlet weak var titleLabel: UILabel! 
} 

image 1:ここでは、collectionViewの最初のtableView(collectionViewの最初の項目)を見ることができます

image 2:ここでは、第二を見ることができます私は現在、以下のコードを使用してい

collectionViewでのtableView(collectionViewの第2項)

image 3:ストーリーボード

image 4:ビュー内の異なるオブジェクトの構造

更新されたコード(@missionManによって答えに応じて - 経由で感謝!)

import UIKit 

class StundenplanCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     navigationController?.navigationBar.prefersLargeTitles = false 
     navigationController?.navigationBar.prefersLargeTitles = true 
     navigationItem.largeTitleDisplayMode = .always 

     myHours.append(myH0) 
     myHours.append(myH1) 
     myHours.append(myH2) 
     myHours.append(myH3) 
     myHours.append(myH4) 
     myHours.append(myH5) 

     collectionView.delegate = self 
     collectionView.dataSource = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    //MARK: Outlets 
    @IBOutlet weak var collectionView: UICollectionView! 

    //MARK: Variables 
    var myDays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"] 
    var myHours: [[String]] = Array() //Array that holds myH1 - myH5 
    var myH0 = ["1", "2"] 
    var myH1 = ["3", "4"] 
    var myH2 = ["5", "6"] 
    var myH3 = ["7", "8"] 
    var myH4 = ["9", "10"] 
    var myH5 = ["Error", "Error"] 

    var headerColor: UIColor = UIColor(red: CGFloat(255/255.0), green: CGFloat(140/255.0), blue: CGFloat(60/255.0), alpha: CGFloat(1.0)) 


    //MARK: Table and Collection View 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return myDays.count 
    } 

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: "stundenplanCollectionViewCell") as? StundenplanCollectionViewCell else { //how can I access the inner tableView? Error: Use of unresolved identifier 'tableView' 
      return UICollectionViewCell() 
     } 

     //set data and reload inner table from outer view controller (StundenplanCollectionViewController according to your code) 
     cell.dayItems = myHours[indexPath.row] 
     cell.tableView.reloadData() 
     //... 

     return cell 
    } 


class StundenplanCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var tableView: UITableView! 
    var dayItems: [String] = [] 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.tableView.delegate = self // <-- set delegates inner table 
     self.tableView.dataSource = self 
    } 
} 

extension StundenplanCollectionViewCell: UITableViewDataSource, UITableViewDelegate { 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return dayItems.count 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 100 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: StundenplanTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "stundenplanTableViewCell") as? StundenplanTableViewCell)! 
     //... 
     return cell 
    } 
} 

class StundenplanTableViewCell: UITableViewCell { 
    @IBOutlet weak var titleLabel: UILabel! 
} 
+2

なぜ、テーブルビューに2つの行しかない場合でも、各セルにテーブルビューを配置していますか?コレクションビューのセルに2つのラベルを直接追加しないでください。 – rmaddy

+0

あなたがデザインしたいもののスクリーンショットを共有します。その後、私はあなたを助けることができます。 – iDev750

+0

@rmaddy:行数はユーザーが変更できるためです。それは単なるサンプルデータです。実際には、テーブルあたり11行のようなものがありそうです。 – Adrian1304

答えて

0

あなたは表のセル内のテーブルのデリゲートメソッドを追加することができますクラス とし、1日分のデータを設定することができます。したがって、各収集セルは独自のテーブルとそれ自身のデータを持ちます。 StundenplanCollectionViewController

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    guard let cell = collectionView.dequeueReusableCell(withIdentifier: "stundenplanCollectionViewCell") as? StundenplanCollectionViewCell else { //<-- HERE 
     return UICollectionViewCell() 
    } 

    //set data and reload inner table from outer view controller (StundenplanCollectionViewController according to your code) 
    cell.dayItems = myH[indexPath.row] 
    cell.innerTableView.reloadData() 
    ... 

    return cell 
} 
+0

まずは:ありがとう!しかし、私は2番目のコード(public func ...)がどこに置かれなければならないのか分かりません。追加情報を提供してください。私は "StundenplanCollectionViewController"内の私の "cellForItemAt"関数をあなたのpublic関数に置き換えました。しかし、Xcodeによると: "未解決の識別子 'tableView'の使用。 – Adrian1304

+0

ようこそ。さて、答えは少し改善されました。お役に立てれば。 – missionMan

+0

それは間違いなく私にとっていくつかのことを明確にしました。どうもありがとう!しかし結局のところ、私はまだあなたのコードを正しい方法で実装することができません。あなたのヒントに従ってコードを更新し、それを質問に追加します。あなたがコードをもう一度見てみるといいですね。** ViewController **(StundenplanCollectionViewController)内のtableViewにアクセスする方法はありません。 – Adrian1304

0

であなたのcollectionViewの外側の細胞から

class StundenplanCollectionViewCell: UICollectionViewCell { //<-- outer cell 
    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var innerTableView: UITableView! //<-- declare inner table 
    var dayItems: [String] = [] 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.innerTableView.delegate = self // <-- set delegates inner table 
     self.innerTableView.dataSource = self 
    } 
} 

extension StundenplanCollectionViewCell: UITableViewDataSource, UITableViewDelegate { 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return dayItems.count 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     ... 
    } 

     ... 

セットデータなぜあなたはUITableViewからdequeueUICollectionViewCellしようとしている。このような

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) 
    ... 
} 
+0

あなたは正しいです、それが更新されたコードの唯一の問題でした。今それは働いている!ありがとう。 – Adrian1304

関連する問題