[英]UICollectionView added inside UITableViewCell programmatically, but doesn't appear?
I have UItableView created from the storyboard and this how I use cellForRowAt indexPath func 我从情节提要中创建了UItableView,这就是我如何使用cellForRowAt indexPath函数
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let titleCell = tableView.dequeueReusableCell(withIdentifier: "titleCell", for: indexPath) as! MenuTableViewCell
titleCell.label.text = newsTitle
return titleCell
}else if indexPath.row == 1 {
let collectionViewCell = tableView.dequeueReusableCell(withIdentifier: "collectionViewCell", for: indexPath) as! collectionViewCell
return collectionViewCell
}
let cell = UITableViewCell()
return cell
}
I have created this class for UICollectionView programmatically : 我已经以编程方式为UICollectionView创建了此类:
private class collectionViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
private let reuseableCell = "CellId"
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseableCell)
}
override func layoutSubviews() {
super.layoutSubviews()
}
let imagesCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.white
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
return collectionView
}()
func setupViews() {
backgroundColor = UIColor.black
addSubview(imagesCollectionView)
imagesCollectionView.dataSource = self
imagesCollectionView.delegate = self
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imagesCollectionView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imagesCollectionView]))
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = UICollectionViewCell()
cell.backgroundColor = .green
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: frame.height - 68)
}
}
as you see I've changed the color of UICollectionView and it's cells, but nothing colored in UITableView. 如您所见,我更改了UICollectionView及其单元格的颜色,但UITableView中没有任何颜色。
and after I run the app nothing happened, so is there any mistake I've made here ? 在我运行该应用程序之后,什么都没发生,那么我在这里犯了任何错误吗?
It doesn't seem like your setupViews()
method is being called. 似乎没有调用
setupViews()
方法。 Consider calling it in your initialisers. 考虑在初始化程序中调用它。
You trying to subclass UITableViewCell
in your UICollectionViewCell
. 您试图在
UICollectionViewCell
UITableViewCell
子类UICollectionViewCell
。
This: 这个:
private class collectionViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate
Should be: 应该:
private class collectionViewCell : UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.