如何根据swift单元格中的图像调整CollectionView单元格高度?

时间:2017-08-14 06:46:33

标签: ios

如何根据显示在其中的图像给CollectionView单元格大小?

1 个答案:

答案 0 :(得分:2)

您需要确认名为UICollectionViewDelegateFlowLayout

的协议
// EXTENSION FOR Controller class
extension Controller:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{


    // NUMBER OF SECTION IN TABLE
    public func numberOfSections(in collectionView: UICollectionView) -> Int{
        return 1
    }

    // NUMBER OF ROWS IN PARENT SECTION
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return Array.count

    }
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

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

        let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ControllerCell
        Cell.menuHeader.text = Array[indexPath.row]
        return parentCell
    }

    // SIZE FOR COLLECTION VIEW CELL
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
            return CGSize(width: view.frame.width, height: 150)
         }
相关问题