arrays - 如何正确地将某些内容附加到数组中?
问题描述
我要制作一个自定义单元格,上面已经有一些标签,然后我创建一个单元格对象和数组,尝试将该对象附加到数组然后显示在表格上,但是在附加之后,我的数组属性中没有内容
我试图找出解决方案,但可能没有人有这些问题
//tableview实现
var recordCell : [RecordCell] = []
let note = RecordCell()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.recordCell.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "recordCell",for: indexPath) as! RecordCell
let indexPath = indexPath.row
cell.recordFileName?.text = self.recordCell[indexPath].recordFileName?.text
cell.recordDate?.text = self.recordCell[indexPath].recordDate?.text
cell.delegate = self
cell.playBtn.tag = indexPath
return cell
}
//追加到数组
let alertController = UIAlertController(title: "請輸入錄音名稱", message: "錄音名稱", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { (_) in
var name : String = ""
if(alertController.textFields![0].text == ""){
name = "record"
}else{
name = alertController.textFields![0].text!
}
guard self.audioRecorder == nil else{return}
self.recordNumber += 1
self.record.isEnabled = false
self.pause.isEnabled = true
self.stop.isEnabled = true
let destinationUrl = self.getFileURL().appendingPathComponent("\(name).m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
self.audioRecorder = try AVAudioRecorder(url: destinationUrl, settings: settings)
self.audioRecorder.record()
self.note.recordFileName?.text = name
self.note.recordDate?.text = self.getDate()
self.recordCell.append(self.note)
} catch {
print("Record error:", error.localizedDescription)
}
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (_) in
self.dismiss(animated: true, completion: {
self.audioRecorder.stop()
})
}
alertController.addTextField { (textField) in
textField.placeholder = "輸入名稱"
textField.keyboardType = .default
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController,animated: true,completion: nil)
}
我希望当我附加一些东西时,数组中有一些东西
解决方案
将新值附加到数组后,调用insertRows
这样的方法
self.recordCell.append(self.note)
self.tableView.insertRows(at: [IndexPath(row: recordCell.count-1, section: 0)], with: .automatic)
推荐阅读
- wpf - 如何解决 XAML 中的“缺少类型”错误?
- javascript - 如何遍历 json 数据并修改访问者名称?
- vaadin - Vaadin Combobox 不显示数据库中的 nameField
- python - 使用最大/最小纬度和经度以及网格点数,如何获得纬度/经度网格?
- java - 使用 CSS 将两个 Vaadin 画布分层
- javascript - 如何循环请求并找到第一个错误
- postgresql - 将 TestNG 开始和停止时间转换为没有时区的 Postgres
- docker - 无法验证证书
因为它不包含任何 IP SAN - python - 显示函数的输出时出现问题
- python - Windows 10 适合用 Python 编程吗?