swift - 如何将数据从 NSTableCellView 传递到自定义 NSTableCellView
问题描述
我正在提取一些 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))%")
}
解决方案
参见NSTableViewDataSource协议,并实现:
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any?
推荐阅读
- java - 继续失败 Spring bean 初始化
- database - 导入 CSV 文件时出现问题 (phpmyadmin)
- flutter - 在 Flutter Video Player 中检测损坏的视频
- c++ - MFC C++ 类成员变量,使用带参数的构造函数
- java - java的WORA概念是针对操作系统还是硬件,Android运行时如何管理它?
- python - 在二维列表中获取正确的值
- python - 使用 tkinter 设计的重置游戏
- ios - UITableView 根据设备显示行数(Swift)
- javascript - 使用 JavaScript 以网格顺序从父网格容器中访问动态添加的 CSS 网格项目?
- python - 如何为函数输出保留多列?