首页 > 解决方案 > 如何在 uiTableViewCell 中添加 collectionView

问题描述

我正在尝试在 uitableView 单元格内添加一个带有 5 个单元格的 uicollectionView。我遇到了集合视图根本不显示的问题。这是我的代码:

// 表视图控制器

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell

        cell.cellIndex = indexPath
        cell.dataSource = self
        cell.delegate = self
        cell.backgroundColor = UIColor.white

//        if let books = all_books[indexPath.section], books.count > 0 {
//            cell.collectionView.reloadData()
//        }
        return cell
    }

// BookTableViewCell

import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage

class BookTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    var delegate: BookTableViewCellDelegate?
    var dataSource: BookTableViewCellDelegate?
    let leftAndRightPaddings: CGFloat = 10.0
    let numberOfItemsPerRow: CGFloat = 5
    let screenSize: CGRect = UIScreen.main.bounds
    var collectionView: UICollectionView!
    var tableRowData: Book!
    var books : [Book]?
    var ref: DatabaseReference!
    var cache:NSCache<AnyObject, AnyObject>!
    var cellIndex: IndexPath!

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

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        flowLayout.itemSize = CGSize(width: 100, height: self.frame.height)
        //You can also provide estimated Height and Width
        flowLayout.estimatedItemSize = CGSize(width: 100, height: self.frame.height)
        //For Setting the Spacing between cells
        flowLayout.minimumInteritemSpacing = 25
        flowLayout.minimumLineSpacing = 20

        collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = UIColor.clear

         self.addSubview(collectionView)
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath) as! BookCollectionViewCell

        self.dataSource?.getData(cell: self)

        let books = self.books
        let book = books?[indexPath.row]

        if book != nil {
            cell.imageView.sd_setImage(with: URL(string: book?.image as! String), placeholderImage: nil)
            cell.label.text = book?.title
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: 100, height: self.frame.height)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.dataSource?.getData(cell: self)
        let books = self.books
        let book = books?[indexPath.row]

        if let book = book {
            self.delegate?.didSelectCollection(book: book)
        }
    }
}

protocol BookTableViewCellDelegate {
    func getData(cell: BookTableViewCell)
    func didSelectCollection(book: Book)
}

有人可以帮忙吗?我在 init 函数中初始化集合视图。正确设置所有代表。不知道我做错了什么。

标签: iosswiftuitableview

解决方案


问题是

collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)

你需要在正确的时间设置框架而不是在init里面

override func layoutSubviews() {
  super.layoutSubviews()
  collectionView.frame = self.bounds
}

或更好地在内部使用约束init,也将其添加到

self.contentView.addSubview(collectionview)

推荐阅读