Expectation outcome的iOS Tableviewcell包含CollectionViewCell
最近,我試圖把收集觀察室到表視圖細胞在某一行。 我設法做到這一點Currently outcome
但不知何故,我不能進一步設計它。 當我打電話
cell.productName.text = "text"
這是我tableviewcell代碼
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
}
}
,這是我收集視圖設計
希望我解釋清楚地解決我的問題。或者給出新的建議來幫助我以另一種方式做到這一點。
感謝
你可以嘗試像這樣http://stackoverflow.com/a/42654038/6433023 –