为啥不去 CollectionViewCell?
Posted
技术标签:
【中文标题】为啥不去 CollectionViewCell?【英文标题】:Why doesn't go CollectionViewCell?为什么不去 CollectionViewCell? 【发布时间】:2020-10-18 13:29:34 【问题描述】:我注意到我调试时 Topics CollectionViewCell 并没有消失。为什么会这样? 所有单元格都正确,但集合视图不可用。
项目的链接如下。你也可以从那里看。
我在这里定义单元格。
extension ArticlesVC: UITableViewDelegate, UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return models.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: CollectionTableViewCell.identifier, for: indexPath) as! CollectionTableViewCell
cell.configure(with: models)
return cell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
return 200
struct Model
let text: String
let imageName: String
init(text: String, imageName: String)
self.text = text
self.imageName = imageName
我在这里定义 CollectionTableViewCell
class CollectionTableViewCell: UITableViewCell
static let identifier = "CollectionTableViewCell"
var collectionView: UICollectionView?
var models = [Model]()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
contentView.addSubview(collectionView!)
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
override func layoutSubviews()
super.layoutSubviews()
collectionView?.frame = contentView.bounds
func configure(with models: [Model])
self.models = models
collectionView?.reloadData()
extension CollectionTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return models.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TopicsCollectionViewCell.identifier, for: indexPath) as! TopicsCollectionViewCell
cell.configure(with: models[indexPath.row])
return cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
return CGSize(width: 250, height: 250)
**This TopicsCollectionViewCell **
class TopicsCollectionViewCell: UICollectionViewCell
static let identifier = "TopicsCollectionViewCell"
let titleLabel: UILabel =
let label = UILabel()
label.text = "label"
return label
()
let photoImage: UIImageView =
let imageView = UIImageView()
return imageView
()
override init(frame: CGRect)
super.init(frame: frame)
contentView.addSubview(titleLabel)
contentView.addSubview(photoImage)
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
override func layoutSubviews()
super.layoutSubviews()
photoImage.frame = CGRect(x: 3,
y: 3,
width: contentView.width - 6,
height: 150)
titleLabel.frame = CGRect(x: 3,
y: photoImage.bottom + 5,
width: contentView.width - 6,
height: contentView.height - photoImage.height - 6)
public func configure(with model: Model)
print(model)
self.titleLabel.text = model.text
self.photoImage.image = UIImage(named: model.imageName)
This link is project with github
【问题讨论】:
【参考方案1】:在执行期间,这些行是无用的,因为collectionView?
是 nil
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self
你需要先初始化collectionView来重新排序代码
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
contentView.addSubview(collectionView!)
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self
【讨论】:
以上是关于为啥不去 CollectionViewCell?的主要内容,如果未能解决你的问题,请参考以下文章