Быстрое несоответствие UICollectionView внутри UITableViewCell - PullRequest
0 голосов
/ 07 февраля 2020
import UIKit

class My_CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!

    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var numberLabel: UILabel!
    @IBOutlet weak var imageV: UIImageView!

    let collectionArray = generateRandomData()

    var index = 0

    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
    }

    func getCellSize(_ targetSize: CGSize) -> CGSize
    {
        return CGSize(width: 44, height: 44)
    }

    override func prepareForReuse()
    {
        self.collectionView.contentOffset = .zero
    }

    override func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize
    {
//        let frame = self.collectionView.frame
//        self.collectionView.layoutIfNeeded()
//
//        let height: CGFloat = self.collectionView.collectionViewLayout.collectionViewContentSize.height
//        self.collectionViewHeight.constant = height
//        self.collectionView.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: height)
//        return self.collectionView.collectionViewLayout.collectionViewContentSize

        self.collectionViewHeight.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
        return self.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    }

    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize
    {
        self.collectionView.layoutIfNeeded()
        self.collectionViewHeight.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
        return self.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    }
}

extension My_CustomTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        let number = Int(self.numberLabel.text!)! + 10
        return number
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollVCell", for: indexPath) as! My_CustomCollectionViewCell
//        let number = Int(self.numberLabel.text!)!
        cell.numberlabel.text = "\(indexPath.row)"

        return cell
    }


}

extension My_CustomTableViewCell: UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: 44, height: 44)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 8.0
    }
}
import UIKit

class My_CustomViewController: UIViewController
{

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Enable automatic row auto layout calculations
        self.tableView.rowHeight = UITableView.automaticDimension
        // Set the estimatedRowHeight to a non-0 value to enable auto layout.
        self.tableView.estimatedRowHeight = 44
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension My_CustomViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 66
    }

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

        cell.collectionView.tag = indexPath.row

        cell.nameLabel.text = "\(indexPath.row)"

        cell.numberLabel.text = "\(indexPath.row)"

        cell.index = indexPath.row

        print("cellForRowAt-----------\(indexPath.row)")
        return cell
    }


}

настройки раскадровки

здесь я не могу получить требуемое количество ячеек сбора, как ожидается, функцией numberOfItemsInSection, которая увеличивается с увеличением количества ячеек таблицы, например 64 элемента коллекции viewview для 64-й ячейки tableview.

Что мне нужно сделать, чтобы получить точное соответствие количества элементов с tableviewcell. ни с одной из вышеперечисленных переменных я не могу отслеживать количество совпадений.

С уважением

...