How to pass data from NSTableCellView to custom NSTableCellView

173 Views Asked by At

I am pulling in some JSON data and displaying the data in an NSTableView. I created a custom cell view called "youCell". In here I am trying to get the percentage to display of a download. This works perfectly if I were to use an actual file which I am not. We are using links that actually initiate a download of a file but it is not an actual file so it cant grab the 'totalBytesExpectedToWrite'.

Inside of my Product object I do have a file size that I want to use to replace that value but I am unsure how to pass it into my custom cell view. I have tried creating a variable in my View Controller and assigning it the prod.product_filesize value but it still always comes back empty when I try and access it in my custom cell view.

I am fairly new to Swift and any guidance would be greatly appreciated.

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))%")


}
1

There are 1 best solutions below

0
Riskov On

See NSTableViewDataSource protocol, and implement:

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