gpt4 book ai didi

ios - 如何让TableView单元格中显示文件名

转载 作者:行者123 更新时间:2023-11-30 11:48:07 25 4
gpt4 key购买 nike

录制的文件没有显示在TableView上我怎样才能显示它?
我希望TableView Cell可以显示recording-yyyy-MM-dd-HH-mm-ss.m4a录音功能正常,但TableView Cell中无法显示文件名
我希望在指定的单元格中也能播放我想听的录音

var recordings = [URL]()

func listRecordings() {

let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let urls = try FileManager.default.contentsOfDirectory(at: documentsDirectory,
includingPropertiesForKeys: nil,
options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
self.recordings = urls.filter({ (name: URL) -> Bool in
return name.pathExtension == "m4a"
})
} catch {
print(error.localizedDescription)
print("something went wrong listing recordings")
}

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recordings.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = recordings[indexPath.row].lastPathComponent
return cell
}

以下是我的存档方式

func setupRecorder() {

let format = DateFormatter()
format.dateFormat="yyyy-MM-dd-HH-mm-ss"
let currentFileName = "recording-\(format.string(from: Date())).m4a"
print(currentFileName)

let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
self.soundFileURL = documentsDirectory.appendingPathComponent(currentFileName)
print("writing to soundfile url: '\(soundFileURL!)'")

if FileManager.default.fileExists(atPath: soundFileURL.absoluteString) {
// probably won't happen. want to do something about it?
print("soundfile \(soundFileURL.absoluteString) exists")
}

最佳答案

您必须重新加载您的tableview

将所有 URL 值存储在 recordings 中后,您必须调用 tableview.reload()

作为初学者,

首先,尝试检查tableView的委托(delegate)和数据源方法是否正在调用。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = String(indexPath.row)
return cell
}

如果上述方法不起作用,请尝试添加以下行。

override func viewDidLoad() {
super.viewDidLoad()

yourTableView.dataSource = self
yourTableView.delegate = self

}

如果一切正常,并且 URL 数组 中有一些 URL,则 reload() 是唯一的问题。

关于ios - 如何让TableView单元格中显示文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605208/

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