Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios can';无法从collectionView中删除项目_Ios_Swift_Uitableview_Uicollectionview - Fatal编程技术网

Ios can';无法从collectionView中删除项目

Ios can';无法从collectionView中删除项目,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我有一个简单的collectionview,它在iphone上显示1列,在iPad上显示2列 有一个按钮用于从collectionview中删除项目,但当我按下该按钮时,出现以下错误: 更新后集合视图中包含的节数(3)必须等于更新前集合视图中包含的节数(4),加上或减去插入或删除的节数(插入0,删除0)。' 这是我的密码 protocol AudioListDataProtocol: class { func search(searchText: String) func pla

我有一个简单的collectionview,它在iphone上显示1列,在iPad上显示2列

有一个按钮用于从collectionview中删除项目,但当我按下该按钮时,出现以下错误:

更新后集合视图中包含的节数(3)必须等于更新前集合视图中包含的节数(4),加上或减去插入或删除的节数(插入0,删除0)。'

这是我的密码

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)
我做错了什么,为什么会崩溃


请帮助我坚持一天。

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


如果您将数据源设置为一个数组数组,其中外部数组表示您的节,而每个内部数组都是该节中的项,那么这将非常有帮助。然后您只需返回数组计数,而不是某个预先确定的变量。

您能纠正“secrtionCount”?;-)的拼写吗@现在我把它修好了。如果可以,请告诉我我的问题我猜是在
self.collectionView.deleteItems(位于:[indexath])
put
self.itemCount-=1
,我现在无法生成您的代码…嗯。明智的解决方案。我的代码上没有任何解决方案?对不起那个人((