2017-06-06 69 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数据源阵列显示所述集合视图。

相关问题