【问题标题】:iOS Tableviewcell contain CollectionViewCelliOS Tableviewcell 包含 CollectionViewCell
【发布时间】: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


【解决方案1】:

查看您的预期输出,我认为您应该使用水平堆栈视图而不是集合视图。它带有 uiview,可以根据您的期望进行自定义。

【讨论】:

  • 对不起,我以前从不使用水平堆栈视图。你能解释一下吗?
  • 你可以看这里raywenderlich.com/114552/…
  • 不,它不能,你必须采取滚动视图添加堆栈视图,其上有 nsviews 的子视图。并且您可以根据需要自定义这些视图。参考在 Mac App Store 中查看 Menucar for uber。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 2013-10-16
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多