【发布时间】:2018-09-17 15:47:38
【问题描述】:
最近,我试图将集合视图单元格放入某一行的表格视图单元格中。 我设法这样做
但不知何故,我无法进一步设计它。 当我打电话时
cell.productName.text = "text"
这是我的表格视图单元代码
class RProductCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{
var collectionView: UICollectionView!
let screenSize = UIScreen.main.bounds
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
let screenWidth = screenSize.width
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
self.bounds = CGRect(x: 90, y: 90, width: screenWidth, height: self.bounds.size.height)
collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CollectionProductCell.self, forCellWithReuseIdentifier: "productcollection")
collectionView.backgroundColor = UIColor.clear
self.addSubview(collectionView)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productcollection", for: indexPath as IndexPath) as! CollectionProductCell
if indexPath.row%2 == 0
{
cell.backgroundColor = UIColor.red
}
else
{
cell.backgroundColor = UIColor.yellow
}
return cell
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这是我的collectionviewcell代码
class CollectionProductCell: UICollectionViewCell {
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var productName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
这是我的收藏视图设计
希望我解释清楚以解决我的问题。或者提供新的建议来帮助我以另一种方式做到这一点。
谢谢
【问题讨论】:
标签: swift uitableview uicollectionview uicollectionviewcell