ios - Swift:如何以编程方式在 TableViewCell 中显示 CollectionView
问题描述
此代码在 tableViewCell 中显示一个 collectionView,其中两个都已添加到 StoryBoard 中:
class TableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 139, height: 64)
}
在我的应用程序中,我正在以编程方式创建 CollectionView 和 TableView。
以编程方式创建一个collectionView:
class collectionViews {
static func collectionViewOne() -> UICollectionView {
let flowLayout = CarouselFlowLayout()
let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)
return collectionViewOne
}
}
在 tableViewCell 中实例化 collectionView:
class TableViewCell2: UITableViewCell {
var moviesItems: [movieItem] = []
let cellIdentifier = "movieCardCell"
let collectionViewOne = collectionViews.collectionViewOne()
private func setupCollectionView(){
collectionViewOne.delegate = self
collectionViewOne.dataSource = self
collectionViewOne.backgroundColor = UIColor (hex: "444A64")
collectionViewOne.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
let nib = UINib(nibName: "movieCardCell", bundle: nil)
collectionViewOne.register(nib, forCellWithReuseIdentifier: cellIdentifier)
}
}
// These functions never get called
extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("INSIDE TableViewCell2.collectionView 1")
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
print("INSIDE TableViewCell2.collectionView 2")
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// For some reason he chose the measures of collectionViewCell and substracted 2
print("INSIDE TableViewCell2.collectionView 3")
return CGSize(width: 139, height: 64)
}
}
注意:委托函数永远不会被调用。所以collectionView从来没有添加到tableViewCell?
以编程方式创建 TableView:
class sectionTableCell: TableViewCell2 {
}
class MoviesViewController4: UIViewController {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor (hex: "444A64")
tableView.delegate = self
tableView.dataSource = self
tableView.register(sectionTableCell.self, forCellReuseIdentifier: "tableViewCell")
displayTableView2()
}
func displayTableView2() {
self.view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
}
extension MoviesViewController4: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell =
tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? sectionTableCell
else {
fatalError("Unable to create explore table view cell")}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 140
}
}
所以问题是: 如何将我以编程方式创建的 collectionView 添加到我也以编程方式创建的 tableViewCell 中?
解决方案
据我所知,您不会setupCollectionView()
在代码中的任何地方调用方法。您应该在init
UITableViewCell 的方法中执行此操作。将此代码添加到您的单元格类中:
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupCollectionView()
}
推荐阅读
- python - 如何将 if 语句用于字典键并填充另一个字典?
- javascript - 如何写出 arr 中的每个值,这是一个字符串数组,每行一个项目
- c++ - 如何使用 CMake 创建包
- python - 如何以有效的方式将列数据添加为行?
- apache-kafka - 尝试使用可为空键模式发布记录时,Kafka REST 代理中的异常“正在注册的模式与早期模式不兼容”
- python-3.x - 我在 Python 上使用 Speech_recognition 包得到 [errno -9999] 意外主机错误
- angular - 如何发送大量请求,但要处理第一个请求的结果,然后使用 angular 和 Observables 发送第二个请求
- javascript - Javascript 表单清理
- react-native - Redux 操作中的异步存储
- c# - 骑自行车者检测球员