1

My numberOfRecords is Int. How do I delete the cell in tableview? Or should I change my way to define my numberOfRecords?

How should I improve my code?

var numberOfRecords = 0

@IBAction func recordButton(_ sender: Any) {
    if audioRecorder == nil {
        numberOfRecords += 1
        let filename = getDirectory().appendingPathComponent("\(numberOfRecords).m4a")
        let settings = [
            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
            AVSampleRateKey: 44100,
            AVNumberOfChannelsKey: 2,
            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
            ]

        do {
            audioRecorder = try AVAudioRecorder(url: filename, settings: settings)
            audioRecorder.delegate = self
            audioRecorder.record()

            recordButton.setTitle("Stop Recording", for: .normal)
        }
        catch
        {
            displayAlert(title: "Ups!", message: "Recording failed")
        }
    }
    else
    {
        audioRecorder.stop()
        audioRecorder = nil

        UserDefaults.standard.set(numberOfRecords, forKey: "myNumber")
        myTableView.reloadData()
        recordButton.setTitle("start Recording", for: .normal)
    }

}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numberOfRecords
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = String(indexPath.row + 1)
    return cell
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let path = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: path)
        audioPlayer.play()
    }catch{

    }
}

In the commissioning editingStyle function inside I should be able to delete the cell inside the tableview as written code

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
蔡家文
  • 33
  • 7
  • try this answer to delete your cell.. https://stackoverflow.com/questions/40156274/deleting-a-row-from-a-uitableview-in-swift-3 – junaid Jan 31 '18 at 04:39
  • My numberOfRecords is int How do I become a string – 蔡家文 Jan 31 '18 at 04:49
  • try : https://stackoverflow.com/questions/48236943/tableview-row-needs-to-de-deleted-twice-before-isnt-removes-form-the-ui/48237487#48237487 – Ashish Sharma Jan 31 '18 at 05:05

2 Answers2

2

Try this one:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        objects.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    } 
}  
kalpesh satasiya
  • 799
  • 8
  • 18
0

You just only show index number on your cell. If you want to delete a cell in any position then you feel one cell is removed from last after reloading the tableview.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
         numberOfRecords -= 1
         tableView.reloadData()
    } 
} 

That's it.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50