刪除項目我有簡單的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)
我在這裏做錯了什麼,爲什麼它崩潰?
請幫助我堅持這一天。
你能改正「secrtionCount」的拼寫嗎? ;-) – onnoweb
@onnoweb我修好了。請告訴我我的問題,如果你可以 – pmb
我的猜測是在'self.collectionView.deleteItems(at:[indexPath])'put'self.itemCount - = 1'之後,我現在無法構建你的代碼... – ViTUu