2017-12-02 277 views
1

我在一个collectionView中放置了一个tableView并添加了代表。问题是,我想控制collectionView中不同tableView中的数据。UITollectionView中的UITableView

每个collectionView都有一个标签,其中包含当天的名称(例如:“Monday”)。标签下面是一个tableView。在tableView中应根据collectionView的索引显示不同的数据。

在我的例子中,每个tableView都有两个单元格。在collectionView的第一项中的tableView应显示:cell1:“1”和cell2:“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:在这里你可以看到第二的tableView在的CollectionView(的CollectionView的第二项)

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

为什么要在每个单元格中放置一个表格视图,如果表格视图只有两行?为什么不直接向收藏夹视图单元添加两个标签? – rmaddy

+0

分享您想要设计的屏幕截图。之后,我可以帮助你。 – iDev750

+0

@rmaddy:因为用户可以更改行数。这只是样本数据。事实上,每桌有11行的可能性更大。 – Adrian1304

回答

0

您可以在表格单元格添加表委托方法 级,然后您可以设置一天的数据。所以每个收集单元都有自己的表格和自己的数据。

是这样的:从您的CollectionView的外细胞在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

首先:感谢您的帮助!但我不确定第二个代码(public func ...)必须放在哪里。你能否提供一些额外的信息?我用公共函数替换了“StundenplanCollectionViewController”中的“cellForItemAt”函数。但Xcode说:“使用未解析的标识符'tableView'”。 – Adrian1304

+0

不客气。好的,答案已经有所改善了。希望这可以帮助。 – missionMan

+0

这对我来说确实使一些事情更清晰。非常感谢!但毕竟我仍然无法以正确的方式实现您的代码。我更新了我的代码,按照你的提示,我将它添加到问题中。如果你可以再看看代码,那将是非常好的。我只是没有得到**如何访问ViewController **(StundenplanCollectionViewController)内的tableView。 – Adrian1304

0

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试图dequeue一个UICollectionViewCell

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

是的,你是对的,这是更新代码的唯一问题。现在它工作了!谢谢。 – Adrian1304