【问题标题】:Swift UICollectionView inside UITableViewCell Programmatically以编程方式在 UITableViewCell 内实现 Swift UICollectionView
【发布时间】:2021-04-24 17:20:30
【问题描述】:

大家好, 我正在尝试以编程方式在 UITableViewCell 中添加 UICollectionView ,但是永远不会调用 collectionView 。我通过代码几次没有运气。我环顾四周也没有运气。我浏览了 Youtube 教程,发现它们也是如此。

你能帮忙吗☹️

    class adsTableViewCell: UITableViewCell {

    var adsImages: [UIImage]? {
        didSet{
            collectionView.reloadData()
        }
    }
    
    let cellID = "Celled"

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.backgroundColor = UIColor.red
        return cv
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        collectionView.register(imageCell.self, forCellWithReuseIdentifier: cellID)

        collectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = false
        
        self.addSubview(collectionView)

    }
    
   

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


extension adsTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! imageCell
        if let imageName = adsImages?[indexPath.item]{
            cell.imageView.image = imageName
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 300, height: frame.height - 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
      
        return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
    }
    
    
    private class imageCell: UICollectionViewCell {
        
        let imageView: UIImageView = {
            let iv = UIImageView()
            iv.contentMode = .scaleAspectFill
            
            iv.clipsToBounds = true
            iv.layer.cornerRadius = 10
            return iv
        }()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setup()
        }
        
        func setup(){
            addSubview(imageView)
            imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
            imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
            imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
          
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}

这是 UITableView,cellForRowAt

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "addViewCell", for: indexPath) as! adsTableViewCell
        cell.adsImages = images
        cell.collectionView.reloadData()
        cell.backgroundColor = UIColor.systemTeal
        cell.selectionStyle = .none
        
        return cell
        
}

【问题讨论】:

  • 设置断点。 awakeFromNib 被调用了吗?您是否尝试将集合视图(并设置约束)添加到单元格的 contentView
  • @Paulw11 感谢您的回复。不,它不被称为 (awakeFromNib)。也是的,我尝试将其添加到 contentView 但仍然相同
  • 如果您的 tableview 单元格未链接到 nib 或故事板,则不会调用 awakeFromNib。你应该覆盖init
  • @Paulw11 我找到了解决方案,谢谢
  • 请不要回答这个问题。我已回滚/编辑您的问题并删除了答案。仅在答案部分添加答案。

标签: ios swift uitableview uicollectionview uicollectionviewcell


【解决方案1】:

我找到了解决方案 只是我不得不改变:

    override func awakeFromNib() {

override required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
    
       setup()
   }

现在一切正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-31
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2017-08-22
    相关资源
    最近更新 更多