【发布时间】:2018-11-01 13:43:56
【问题描述】:
我有 UITableView 来显示文件列表。每个文件名都使用特定的代码组合进行编码,为了获得真实的文件名,我必须使用当前文件名调用我的服务器端。
是否可以在 cellForRowAt indexPath 表视图委托函数上调用这样的操作?
var files: [URL]!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "FilesManagerCell", for: indexPath) as? FilesManagerCell {
let currentFile = files[indexPath.row]
APIClient.getFileInfo(patientId: currentFile.lastPathComponent, onSuccess: { (response) in
cell.filenameLabel.text = response.file.fileName
cell.createdDateLabel.text = response.file.creationDate
cell.fileSizeLabel.text = response.file.fileSizeWithToken
cell.filePath = currentFile
return cell <- Here I get expected error
}, onError: { (errorResponse, errorString) in
})
}
return UITableViewCell()
【问题讨论】:
-
将您的请求移至 FilesManagerCell 类,以便它可以异步处理请求。如果单元被重用,您还需要覆盖
prepareForReuse以停止请求。所以cellForRowAt函数应该只配置cell,可以创建downloadFile(_ filePath: String)之类的函数来处理API请求。 -
除此之外无论如何都不能工作,不要那样做。
cellForRow用于更新和显示 UI 元素。在viewDidLoad/viewWillAppear甚至模型中执行异步任务。考虑到用户滚动时可以随时释放单元格。
标签: ios swift uitableview