在我的應用程序中,用戶可以錄製音頻(如語音備忘錄)。完成記錄後,需要用戶輸入記錄名稱,並在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
}
你有音頻的日期在一個陣列?如果是,則共享示例日期數組。 –
不...兄弟。我沒有 –
如果你沒有,你願意根據日期排序嗎? –