如何动态扩展xib collectionviewcell的高度?

时间:2019-05-22 17:11:15

标签: swift uicollectionview uicollectionviewcell xib uicollectionviewlayout

我希望在单击时使我的xib自定义集合视图单元格高度动态,而不是使用固定的可变高度。

我尝试使用对情节提要中单元格高度的约束。

这是MotCollectionViewCell.swift代码:

import UIKit

protocol ExpandedCellDelegate:NSObjectProtocol{
    func topButtonTouched(indexPath:IndexPath)
}

class MotCollectionViewCell: UICollectionViewCell {

    @IBOutlet var heightConstraint: NSLayoutConstraint!
    @IBOutlet var topButton: UIButton!
    weak var delegate:ExpandedCellDelegate?

    public var indexPath:IndexPath!


    @IBAction func topButtonTouched(_ sender: UIButton) {

        if let delegate = self.delegate{
            delegate.topButtonTouched(indexPath: indexPath)

        }
    }

    @IBOutlet var testLbl: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code    
    }
}

这是BarChartViewControllerCell.Swift代码:

import UIKit
import Charts

class BarChartViewController: UIViewController, ChartViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ExpandedCellDelegate {



    // Bar Chart Properties
    @IBOutlet var barChartView: BarChartView!

    var dataEntry: [BarChartDataEntry] = []

    // Chart Data
    var result =  [String]()
    var mileage = [String]()
    var colours = [UIColor]()

    var list = [Tester]()

    // Collection View properties


    @IBOutlet var collectionView: UICollectionView!

    var expandedCellIdentifier = "MotCollectionViewCell"

    var cellWidth:CGFloat{
        return collectionView.frame.size.width
    }
    var expandedHeight : CGFloat = 258
    var notExpandedHeight : CGFloat = 75

    var dataSource = ["data0","data1","data2","data3","data4"]
    var isExpanded = [Bool]()

    override func viewDidLoad() {
        super.viewDidLoad()

        isExpanded = Array(repeating: false, count: dataSource.count)

        //Register nib cell
        let nibCell = UINib(nibName: expandedCellIdentifier, bundle: nil)
        collectionView.register(nibCell, forCellWithReuseIdentifier: expandedCellIdentifier)

}

  // Collection View functions

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: expandedCellIdentifier, for: indexPath) as! MotCollectionViewCell
        cell.indexPath = indexPath
        cell.delegate = self
        //configure Cell
        return cell

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if isExpanded[indexPath.row] == true{
            return CGSize(width: cellWidth, height: expandedHeight)
        }else{
            return CGSize(width: cellWidth, height: notExpandedHeight)
        }

    }

    func topButtonTouched(indexPath: IndexPath) {
        isExpanded[indexPath.row] = !isExpanded[indexPath.row]
        UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIView.AnimationOptions.curveEaseInOut, animations: {
            self.collectionView.reloadItems(at: [indexPath])
        }, completion: { success in
            print("success")
        })
    }
}

单击该单元格时,该单元格会扩展到我声明为常数的高度,我要达到的目的是使该单元格根据其中的数据进行扩展。

0 个答案:

没有答案