【发布时间】:2020-03-09 16:14:24
【问题描述】:
我正在提取一些 JSON 数据并在 NSTableView 中显示这些数据。我创建了一个名为“youCell”的自定义单元格视图。在这里,我试图获取显示下载的百分比。如果我要使用我不是的实际文件,这将非常有效。我们正在使用实际启动文件下载的链接,但它不是实际文件,因此它无法获取“totalBytesExpectedToWrite”。
在我的 Product 对象内部,我确实有一个文件大小,我想用它来替换该值,但我不确定如何将它传递到我的自定义单元格视图中。我尝试在我的视图控制器中创建一个变量并为其分配 prod.product_filesize 值,但是当我尝试在我的自定义单元格视图中访问它时它仍然总是空的。
我对 Swift 还很陌生,非常感谢任何指导。
import Cocoa
import Kingfisher
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var tableView: NSTableView!
var products = [Product]()
var passFileSize = ""
struct Product: Decodable {
let product_name: String
let product_image: String
let order_id: String?
let download_string: String
let product_link: String?
let validation_email: String?
let product_filesize: String
}
func numberOfRows(in tableView: NSTableView) -> Int {
return (products.count)
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let prod = products[row]
if tableColumn?.identifier == NSUserInterfaceItemIdentifier(rawValue: "pNameColumn") {
let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "nameCell")
guard let cellView = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? NSTableCellView else { return nil }
cellView.textField?.stringValue = prod.product_name
return cellView
}else if tableColumn?.identifier == NSUserInterfaceItemIdentifier(rawValue: "pFileColumn") {
let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "pFileCell")
guard let cellView = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? youCell else { return nil }
cellView.textField?.stringValue = prod.product_filesize
passFileSize = prod.product_filesize
cellView.yourobj = {
cellView.animateCAShapeLayerDrawing(theFile: self.urlTestString)
}
return cellView
}else if tableColumn?.identifier == NSUserInterfaceItemIdentifier(rawValue: "pImageColumn") {
let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "pImageCell")
guard let cellView = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? NSTableCellView else { return nil }
let stringer = prod.product_image
let url = URL(string: stringer)
cellView.imageView?.kf.setImage(with: url)
return cellView
}
return view
}
}
class youCell: NSTableCellView, URLSessionDownloadDelegate
{
var vc = ViewController()
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Finished in youCell")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let newFileSize = vc.passFileSize
print(newFileSize)
//replace totalBytesExpectedToWrite with newFileSize
let percentage = CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)
print("\(Int(percentage * 100))%")
}
【问题讨论】:
-
永远不要在 view 中运行异步任务,当用户滚动时可以立即释放单元格视图。在 controller 或 model. 中执行此操作。除此之外,如果您使用的是故事板或 XIB,
ViewController()不是您期望的实例。 -
我对此感到很困惑。我有自定义视图的原因是因为我使用下载按钮来启动下载进度的自定义视图内的动画。我尝试在视图控制器内部进行自定义视图,但它一直说我无法在可重复内容中进行自定义视图。所以我不确定如何正确地做到这一点。
标签: swift nstablecellview