2017-08-14 380 views
-1

如何调整CollectionView自定义单元格的大小,以及此代码使用的是什么,请大家帮忙,因为我是iOS新手。 在此先感谢。在Swift中调整CollectionView自定义单元格大小的代表

+0

的CollectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath。用这种方法,而且按照这个[缩放链接](https://stackoverflow.com/questions/40974973 /如何调整收集视图细胞根据设备的屏幕大小) –

+0

我已经使用它,但我的控制不会去那个方法..... –

+0

检查无论你是否设置了collectionView委托。 –

回答

1

您需要确认协议称为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) 
     } 
+0

是的,现在它正在使用该方法并更改单元格的大小。非常感谢。 –

+0

@VivekTyagi没问题 –

相关问题