'无效的更新:我的收藏夹视图中第0部分中的项目数无效

时间:2018-12-07 16:49:02

标签: ios swift uicollectionview

当我在应用程序上按editButton并尝试删除collectionView中的单元格时,会收到以下错误消息:

  

'无效更新:第0部分中的项目数无效。   更新(3)之后现有部分中包含的项目必须是   等于该节之前该部分中包含的项目数   更新(3),加上或减去从中插入或删除的项目数   该部分(插入了0个,删除了1个),加上或减去   项目移入或移出了该部分(移入了0个,移出了0个)。'

通读other similar posts之后,我仍然不确定如何为该问题应用解决方案。我确实看到文件已从FileManager中删除,因为我已经在Finder中打开了目录,但是我对如何使collectionView与FileManager中的文件数量保持同步感到困惑。

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioRecorderDelegate, UICollectionViewDelegate, UICollectionViewDataSource {

    var audioRecorder: AVAudioRecorder!
    var audioPlayer: AVAudioPlayer!
    var numberOfRecordings = 0

    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var editButton: UIButton!
    @IBOutlet weak var myCollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        recordButton.layer.cornerRadius = 10
        editButton.layer.cornerRadius = 10


         // Set the numberOfRecordings to be exactly the number of files stored in the File Manager so that they're in sync.
        let fileManager = FileManager.default
        let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        do {
            let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
            numberOfRecordings = fileURLs.count
        } catch {
            print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
        }

    }

    // Let's get the directory where we're going to store the recordings
    func getDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = paths[0]
        return documentDirectory
    }

    // Let's create a genearl alert to display error messages
    func displayAlert(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }

    //MARK: - Record Button Methods
    @IBAction func record(_ sender: Any) {

        if audioRecorder == nil {
            numberOfRecordings += 1
            let fileURL = getDirectory().appendingPathComponent("\(numberOfRecordings).m4a")
            let settings = [
                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 12000,
                AVNumberOfChannelsKey: 1,
                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
            ]

            do {
                audioRecorder = try AVAudioRecorder(url: fileURL, settings: settings)
                audioRecorder.delegate = self
                audioRecorder.record()
                recordButton.setTitle("Stop Recording", for: .normal)
            } catch {
                displayAlert(title: "Oops!", message: "Recording Failed")
            }
        } else {
            audioRecorder.stop()
            audioRecorder = nil
            UserDefaults.standard.set(numberOfRecordings, forKey: "numberOfRecordings")
            recordButton.setTitle("Start Recording", for: .normal)
            myCollectionView.reloadData()

        }

    }


    //MARK: - Collection View Setup
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return numberOfRecordings
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! RecordingCollectionViewCell
        cell.recordingLabel.text = String(indexPath.row + 1)
        cell.layer.cornerRadius = 10
        cell.delegate = self as RecordingCellDelegate
        return cell
    }

    //MARK: - Audio Player
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let path = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: path)
            audioPlayer.volume = 1.0
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        } catch let error {
            print("Error: \(error.localizedDescription)")
        }
    }

    //MARK: - Edit Button Methods
    @IBAction func editButtonTapped(_ sender: Any) {
        if editButton.titleLabel?.text == "Edit" {
            recordButton.isEnabled = isEditing
            editButton.setTitle("Stop Editing", for: .normal)
            if let indexPaths = myCollectionView?.indexPathsForVisibleItems {
                for indexPath in indexPaths {
                    if let cell = myCollectionView?.cellForItem(at: indexPath) as? RecordingCollectionViewCell {
                        cell.isEditing = !isEditing
                    }
                }
            }
        } else {
            editButton.setTitle("Edit", for: .normal)
            if let indexPaths = myCollectionView?.indexPathsForVisibleItems {
                for indexPath in indexPaths {
                    if let cell = myCollectionView?.cellForItem(at: indexPath) as? RecordingCollectionViewCell {
                        cell.isEditing = isEditing
                    }
                }
            }
        }


    }


}


extension ViewController: RecordingCellDelegate {
    func delete(cell: RecordingCollectionViewCell) {
        if let indexPath = myCollectionView?.indexPath(for: cell) {
            // 1. Delete the recording from the File Manager
            let fileManager = FileManager.default
            let fileURL = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")
            do {
                try fileManager.removeItem(at: fileURL)

            }   catch let error {
                print("File not found: \(error.localizedDescription)")
                displayAlert(title: "Oops!", message: "File not found: \(error.localizedDescription)")
            }

            // 2. Delete it in the collectionView
            myCollectionView.deleteItems(at: [indexPath])
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您永远不会更新数据模型。除了删除文件并告诉集合视图某个项目已被删除之外,您还需要更新集合视图使用的模型。这意味着您需要更新numberOfRecordings(在调用myCollectionView.deleteItems(at: [indexPath])之前。

答案 1 :(得分:0)

错误明确指出,要添加新行或删除现有行,您还需要对相应的数组进行相同的更改,即 numberOfRecordings

像,如果您要删除1行,则需要对数组进行相同的更改(例如,之前有3行(数组也有3项),并且现在您要删除1行,因此您需要从相应的array(numberOfRecordings)中删除相同数量的项(即1)。

同样也适用于添加。

  

在对数组进行任何更改之前,执行对数组的添加或删除操作   带有tableView.deleteRows()的表视图或   tableView.insertRows()