2017-06-20 33 views
0

删除项目我有简单的CollectionView这表明在iphone 1列,并在iPad上不能的CollectionView

2列

有一个按钮从的CollectionView删除项目,但是当我按下按钮,我得到这个错误:

The number of sections contained in the collection view after the update (3) must be equal to the number of sections contained in the collection view before the update (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

这是我的代码

protocol AudioListDataProtocol: class { 
    func search(searchText: String) 
    func playComposition(composition: Composition, data: [Composition], play: Bool) 
    func endEditing() 
} 

class AudioListDataAdapter: NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, AudioItemCellProtocol { 
    var delegate: AudioListDataProtocol! 
    var isFavoriesList = false 

    private var data = [Composition]() 
    private var collectionView: UICollectionView! 
    private var searchBar: UISearchBar! 
    private let cellIdentifier = "AudioCellIdent" 
    private var selectedIndexPath: IndexPath! 
    private var cellWidth = 0 as CGFloat 
    private var sectionCount = 0 
    private var itemCount = 0 


    //MARK: public methods 
    func initSearchBar(searchBar: UISearchBar) -> Void { 
     self.searchBar = searchBar 
     self.searchBar.delegate = self 
     self.searchBar.returnKeyType = .done 
     self.searchBar.placeholder = DicionaryManager.shared.getStringValue(dKey: AMKeys.mobile_label_search_here) 
    } 

    func initCollection(collection: UICollectionView) -> Void { 
     self.collectionView = collection 
     self.collectionView.dataSource = self 
     self.collectionView.delegate = self 
     self.collectionView.register(UINib.init(nibName: "AudioItemCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier) 
    } 

    func setListData(data: [Composition], itemsPerSection: Int, itemWidth: CGFloat) -> Void { 
     cellWidth = itemWidth 
     self.data = data 
     itemCount = itemsPerSection 
     sectionCount = Int(ceil(Double(self.data.count/itemCount))) 
    } 

    func reload() { 
     collectionView.reloadData() 
    } 


    //MARK: UISearchBarDelegate 
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 
     searchBar.showsCancelButton = true 
    } 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     if searchText.characters.count >= 3 { 
      if let d = delegate { 
       d.search(searchText: searchText) 
      } 
     } 
    } 

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
     searchBar.text = "" 
     if let d = delegate { 
      d.search(searchText: "") 
      d.endEditing() 
     } 
    } 

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 
     if let d = delegate { 
      d.endEditing() 
     } 
    } 


    //MARK: UICollectionViewDataSource 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return sectionCount 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return itemCount 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? AudioItemCell { 
      return cell 
     } 
     return UICollectionViewCell() 
    } 

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
     let c = cell as! AudioItemCell 
     c.setupComposition(composition: data[curItemIndex(indexPath: indexPath)]) 
     c.indexPath = indexPath 
     c.favoriteState = isFavoriesList 
     c.delegate = self 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     PlayerManager.shared.changePlaylist() 
     var item: Composition 
     item = data[curItemIndex(indexPath: indexPath)] as Composition 
     item.isPlaying = true 
     item.showPlayer = true 
     collectionView.reloadItems(at: [indexPath]) 
     selectedIndexPath = indexPath 
     delegate.playComposition(composition: item, data: data, play: true) 
    } 

    func playingCompositionForReload(composition: Composition) { 
     composition.isPlaying = true 
     composition.showPlayer = true 
     collectionView.reloadData() 
    } 


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: cellWidth, height: 90) 
    } 


    //MARK: helpers methods 
    func updateState(play: Bool) -> Void { 
     if (selectedIndexPath) != nil { 
      let item = data[curItemIndex(indexPath: selectedIndexPath)] 
      item.isPlaying = !item.isPlaying 
     } else { 
      selectedIndexPath = IndexPath(row: 0, section: 0) 
      let item = data[0] 
      item.isPlaying = true 
      item.showPlayer = true 
     } 

     collectionView.reloadItems(at: [selectedIndexPath]) 
    } 


    private func curItemIndex(indexPath: IndexPath) -> Int { 
     return indexPath.section * itemCount + indexPath.item 
    } 


    //MARK: AudioItemCellProtocol 
    func removeCellAtIndexpath(indexPath: IndexPath) { 
     collectionView.performBatchUpdates({ 
         self.data.remove(at: self.curItemIndex(indexPath: indexPath)) 
      sectionCount = Int(ceil(Double(self.data.count/self.itemCount))) 
      self.collectionView.deleteItems(at: [indexPath]) 
     }, completion: nil) 
    } 
} 

,这个按钮按下代码

delegate.removeCellAtIndexpath(indexPath: indexPath) 

我在这里做错了什么,为什么它崩溃?

请帮助我坚持这一天。

+0

你能改正“secrtionCount”的拼写吗? ;-) – onnoweb

+0

@onnoweb我修好了。请告诉我我的问题,如果你可以 – pmb

+0

我的猜测是在'self.collectionView.deleteItems(at:[indexPath])'put'self.itemCount - = 1'之后,我现在无法构建你的代码... – ViTUu

回答

0

问题出在您的numberOfItemsInSection方法和使用itemCount属性。你正在返回一个固定的号码。你假设每个部分都有相同的固定数量的项目。相反,您应该返回给定部分的数据源中的当前记录数。

如果将数据源设置为数组数组,其中外部数组表示您的部分,并且每个内部数组都是该部分中的项目,这将有很大帮助。然后你只需返回数组数而不是某个预定的变量。

+0

什么是我的代码解决方案? (( – pmb

+0

查看我的更新回答。) – rmaddy

+0

hmmm。明智的解决方案。我的代码上没有任何解决方案吗? – pmb

相关问题