gpt4 book ai didi

ios - UICollectionView 中的 UITableView

转载 作者:行者123 更新时间:2023-11-28 07:57:36 26 4
gpt4 key购买 nike

我在 collectionView 中放置了一个 tableView 并添加了委托(delegate)。问题是,我想控制 collectionView 中不同 tableViews 中的数据。

每个 collectionView 都有一个带有当天名称的标签(例如:“星期一”)。标签下方是一个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!
}

图 1: 这里可以看到 collectionView 中的第一个 tableView(collectionView 的第一项) :

image 1

图2:这里可以看到collectionView中的第二个tableView(collectionView的second Item):

image 2

图 3: Storyboard:

image 3

图 4: View 中不同对象的结构:

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!
}

最佳答案

您可以在表格单元格类中添加表格委托(delegate)方法然后你可以设置一天的数据。因此每个集合单元格都有自己的表格和数据。

像这样:

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 {
...
}

...

StundenplanCollectionViewController 中从 collectionView 的外部单元格设置数据

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
}

关于ios - UICollectionView 中的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47610540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com