2016-08-12 53 views
0

如何從控制器視圖中刪除單個部分?我在標題中有一個按鈕,並且所有內容都已連接。只是不確定如何編寫3個不同部分的代碼。如何刪除集合視圖中的部分

我的數據模型

var fire = [UIImages] 
var water = [UIImages] 
var air = [UIImages] 

var fireLabel = [String] 
var waterLabel = [String] 
var airLabel = [String] 

我的手機配置代碼

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if indexPath.section == 0 { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

     cell.fire.image = fireImages[indexPath.row] 
     cell.fireLabel.text = fireNames[indexPath.row] 


    return cell 

    } else if indexPath.section == 1 { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

     cell.water.image = waterImages[indexPath.row] 
     cell.waterLabel.text = waterNames[indexPath.row] 

     return cell 

    } else { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

     cell.air.image = airImages[indexPath.row] 
     cell.airLabel.text = airNames[indexPath.row] 

     return cell 

    } 

} 

這裏是我的按鈕的代碼,它在每一個標題。我想要做的是當你點擊這個按鈕時,它會刪除整個部分。再次爲每個。但我似乎無法讓它工作。

//Delete Section Button 
@IBAction func deleteSectionButton(sender: UIButton) { 

    //Section tag 
    let section = sender.tag 

    //Update data model 
    fireImages.removeAtIndex(section) 
    fireNames.removeAtIndex(section) 

    self.collectionView?.deleteSections(NSIndexSet(index: section)) 

} 

我得到這個錯誤:

***終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「無效的更新:部分無效號碼。更新後集合視圖中包含的部分數量(3)必須等於更新前集合視圖中包含的部分數量(3),加上或減去插入或刪除的部分數量(插入0,1刪除)。」

但我不知道這是什麼意思。

+0

您是指所選行或整個部分? –

+0

整個部分..對於遲到的響應後悔 – art3mis

回答

0

您可以在更新數據後重新加載收集視圖。它會根據收集視圖按比例填充數據

self.collectionView.reloadData(); 
+0

問題是更新數據。我有3個陣列,我不知道這將如何在一個按鈕上工作。 – art3mis

+0

我已經在另一個問題上解釋了[解決方案](http://stackoverflow.com/questions/38944113/delete-button-not-working-collectionview)。你有沒有時間看它? – alptugd