【发布时间】:2017-05-25 20:31:37
【问题描述】:
在我的应用中,用户可以录制音频(如 语音备忘录)。录制完成后,需要用户输入给录制文件命名,音频显示在UITableView。录制的音频按其名称(按字母顺序)排序。我需要按创建日期对它们进行排序 - 最后创建的音频将首先出现。我使用了两个数组 -
1.recordedAudioFilesURLArray(类型:URL)& 2.recordedAudioFileName(类型:字符串)。
录制的音频保存在文档目录中。这是我的代码示例...
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
}
【问题讨论】:
-
你有音频的日期吗?如果是,则共享示例日期数组。
-
不...兄弟。我没有
-
如果没有日期,你愿意如何按日期排序?
-
@pigeon_39 其实你可以通过
FileManagerapi获取文件创建日期
标签: ios swift uitableview sorting