首页 > 解决方案 > 如何将数据从 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))%")


}

标签: swiftnstablecellview

解决方案


参见NSTableViewDataSource协议,并实现:

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any?

推荐阅读