gpt4 book ai didi

ios - 如何删除tableview的单元格?

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:06 26 4
gpt4 key购买 nike

我的 numberOfRecords 是整数。如何删除tableview中的单元格?或者我应该改变我的方式来定义我的 numberOfRecords?

我应该如何改进我的代码?

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{

}
}

在调试里面的editingStyle函数我写的代码应该可以删除tableview里面的cell

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}

最佳答案

试试这个:

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)
}
}

关于ios - 如何删除tableview的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48534512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com