我试图在TableViewCell中设置一个CollectionView . 我已经阅读了一堆完整的堆栈问题,tuts和视频,到目前为止,我有一些似乎正确的方法,但我的集合视图仍未加载到我的表格视图单元格中 .
码:
import UIKit
class TheaterCell: UITableViewCell {
@IBOutlet var theaterName: UILabel!
@IBOutlet var theaterLocation: UILabel!
@IBOutlet var showtimesCollection: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
showtimesCollection.delegate = self
showtimesCollection.dataSource = self
showtimesCollection.reloadData()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension TheaterCell: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = showtimesCollection.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! TimeCell
cell.time.text = "1:00"
return cell
}
}
Tableview从ViewController加载并显示其单元格和元素,但集合视图未在单元格中加载 .
2 回答
我做什么,我使用storyboard并通过拖入类来设置Storyboard中的委托和数据源 .
a)将TableView的委托和数据源设置为ViewController
b)将CollectionView的委托和数据源设置为TableViewCell(TheaterCell)
ViewController代码:
TheaterCell代码:
TimeCell代码:
这是输出:
NOTE: IF YOU ARE NOT USING STORYBOARDS AND YOU MAKE COLLECTIONVIEW OR TABLE FROM CODE ONLY, THEN YOU HAVE REGISTER YOURS CELL AS: A) TheaterCell must be registered into ViewController class B) TimeCell must be registered into TheaterCell class
这对我有用,我认为问题来自你注册你的细胞的方式