2017-06-06 67 views
-2

我有一個自定義集合視圖在Viewcontroller中,每當我的集合視圖加載時總是讓我的第一個單元格爲空。如何刪除這個空單元格。集合視圖有第一個單元格總是空的

enter image description here

//CollectionView functions 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return collectionData.count 
} 

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

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

    let collectionData = self.collectionData[indexPath.row] 


    cell.NameLbl.text  = collectionData["name"] as? String 



    return cell 

} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    let Info: NSDictionary = self.collectionData[indexPath.item] as! NSDictionary 


    let vc = self.storyboard?.instantiateViewController(withIdentifier: "edit") as! editVC 
    self.navigationController?.pushViewController(vc, animated: false) 



} 
+0

到目前爲止顯示您的嘗試代碼? –

+0

所有數據都是動態的?顯示您的collectionview數據源Logic – vivek

+0

是您檢查了您的數據源嗎? – Jaydeep

回答

0

要看是什麼,你應該處理隱藏的第一個元素的情況下,有一些你可能想要實施拖選項:

如果它是好的從數據源中刪除第一個對象(collectionData數組),然後您可以簡單地從中刪除第一個元素:

在您的視圖控制器(viewDidLoad()),你可以實現:

override func viewDidLoad() { 
    . 
    . 
    . 

    collectionData.remove(at: 0) 

    . 
    . 
    . 
} 

2-,如果你需要保持collectionData因爲無需拆卸的第一要素,但它不應該被顯示在用戶界面,你將需要實現UICollectionViewDataSource如下:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    // subtract 1 
    return collectionData.count - 1 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! collectionCell 

    // add 1 to indexPath.row 
    let collectionData = self.collectionData[indexPath.row + 1] 

    cell.NameLbl.text = collectionData["name"] as? String 

    return cell 

} 

這應該導致預期無需編輯collectionData數據源陣列顯示所述集合視圖。

相關問題