UICollectionView 以编程方式添加到 UITableViewCell 中,但没有出现?
UICollectionView added inside UITableViewCell programmatically, but doesn't appear?
我从故事板创建了 UItableView,这就是我如何使用 cellForRowAt indexPath func
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
}
我以编程方式为 UICollectionView 创建了这个 class:
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)
}
}
如您所见,我已经更改了 UICollectionView 及其单元格的颜色,但 UITableView 中没有任何颜色。
在我 运行 应用程序之后没有任何反应,所以我在这里犯了什么错误吗?
您的 setupViews()
方法似乎没有被调用。考虑在您的初始化程序中调用它。
您试图在 UICollectionViewCell
中继承 UITableViewCell
。
这个:
private class collectionViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate
应该是:
private class collectionViewCell : UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate
我从故事板创建了 UItableView,这就是我如何使用 cellForRowAt indexPath func
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
}
我以编程方式为 UICollectionView 创建了这个 class:
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)
}
}
如您所见,我已经更改了 UICollectionView 及其单元格的颜色,但 UITableView 中没有任何颜色。
在我 运行 应用程序之后没有任何反应,所以我在这里犯了什么错误吗?
您的 setupViews()
方法似乎没有被调用。考虑在您的初始化程序中调用它。
您试图在 UICollectionViewCell
中继承 UITableViewCell
。
这个:
private class collectionViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate
应该是:
private class collectionViewCell : UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate