1

Expectation outcomeiOSの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 
} 

} 

であり、これは私のコレクションビューのデザインである

希望私が説明明らかに私の問題を解決する。または、別の方法でこれを行うのに役立つ新しい提案をしてください。あなたの期待出力を見てみると

おかげ

+0

このようにすることができますhttp://stackoverflow.com/a/42654038/6433023 –

答えて

0

は、私はあなたがview.Itはあなたの期待に応じてカスタマイズすることができるのUIViewに付属している水平スタックビューではなく、コレクションを使用すべきだと思います。

+0

申し訳ありませんが、前に水平のスタックビューを使用していません。あなたはそれについてもっと説明できますか? –

+0

ここではhttps://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views –

+0

を見ることができますが、水平方向のスタックビューは右にスクロールできません。 –

関連する問題