在我的应用程序中,用户可以录制音频(如语音备忘录)。完成记录后,需要用户输入记录名称,并在UITableView
中显示音频。录制的音频按其名称排序(按字母顺序)。我需要按创建日期对它们进行排序 - 最后创建的音频将首先出现。我使用了两个阵列 -Swift - 按创建日期排序表单元格
1.recordedAudioFilesURLArray(Type:URL)& 2.recordedAudioFileName(Type:String)。
录制的音频保存在文档目录中。这里是我的代码示例...
func getRecordedAudioFilesFromDocDirectory() {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
recordedAudioFilesURLArray = directoryContents.filter{ $0.pathExtension == "m4a" }
} catch let error as NSError {
print(error.localizedDescription)
}
recordedAudioFileNames = recordedAudioFilesURLArray.flatMap({$0.deletingPathExtension().lastPathComponent})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recordedAudioFilesURLArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = recordedAudioFileNames[indexPath.row] as! NSString as String
return cell
}
你有音频的日期在一个阵列?如果是,则共享示例日期数组。 –
不...兄弟。我没有 –
如果你没有,你愿意根据日期排序吗? –