Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

swift - NSTableViewDelegate and NSTableViewSource

I am learning Swift and have experience with Visual Basic and C++. But, I am completely confused on NSTableView. This code works, but I have two NSTableView controls on my View Controller. These need to have their own class to handle their data.

The delegate and datasource of each table view need to be set to their own classes (instead of self) because they will store different data sets. I have tried writing my own class, but I get errors about scopes.

How do I implement that class?

Illustration of What I Want

override func viewDidLoad() {
super.viewDidLoad()

// YOUTUBE VIDEOS
tableView.delegate = classVideos // instead of using self
tableView.dataSource = classVideos // instead of using self

// RESOURCES
tableView1.delegate = classResources // instead of using self
tableView1.dataSource = classResources // instead of using self

tableView.reloadData()
DataManager.shared.clear()
DataManager.shared.save()

}

Current View Controller

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {

    // FIELDS FOR TABLEVIEW
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var col_Icon: NSTableColumn!
    @IBOutlet weak var txtName: NSTextField!
    @IBOutlet weak var txtURL: NSTextField!
    @IBOutlet weak var txtTime: NSTextField!
    @IBOutlet weak var txtIcon: NSTextField!

    // FIELDS FOR TABLEVIEW2
    @IBOutlet weak var tableView2: NSTableView!
    @IBOutlet weak var btnResources_Add: NSButton!
    @IBOutlet weak var txtResource_Icon: NSTextField!
    @IBOutlet weak var txtResource_Name: NSTextField!
    @IBOutlet weak var txtResource_URL: NSTextField!

@IBAction func Click(_ sender: NSButton) {
    if txtURL.stringValue.count > 0 {
        let todo = TodoItem(icon: txtIcon.stringValue, name: txtName.stringValue, url: txtURL.stringValue, time: txtTime.stringValue)
        DataManager.shared.add(item: todo)
        tableView.reloadData()
    }
}


override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self
    tableView.dataSource = self
    
    tableView.reloadData()
    DataManager.shared.clear()
    DataManager.shared.save()
    
}

Continued from View Controller (above) Portion I Need in a Class

// MARK: TableViewDataSource and TableViewDelegate

func numberOfRows(in tableView: NSTableView) -> Int {
    return DataManager.shared.storedTodos.count
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    
    if row < self.numberOfRows(in: tableView) {
        
        let todo = DataManager.shared.storedTodos[row]
        
        var cellValue = ""
        var cellIdentifier = NSUserInterfaceItemIdentifier("")
        
        let tableIdentifier = tableView.identifier
        
        
        switch tableColumn?.identifier.rawValue {
        
        case "Icon":
            cellValue = todo.Icon
            cellIdentifier = NSUserInterfaceItemIdentifier("tsIcon")
        
        case "URL":
            cellValue = todo.URL
            cellIdentifier = NSUserInterfaceItemIdentifier("tsURL")
        
        case "Time":
            cellValue = todo.Time
            cellIdentifier = NSUserInterfaceItemIdentifier("tsTime")
            
        case "Name":
            cellValue = todo.Name
            cellIdentifier = NSUserInterfaceItemIdentifier("tsName")
            
        default:
            print("Error! No rawvalue for " ?? tableColumn?.identifier.rawValue)
        
        }
    
        
    if let cell = tableView.makeView(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {
        
        cell.textField?.stringValue = cellValue
        
        //if todo.todoDueDate.compare(Date()) == .orderedAscending {
        //    cell.alphaValue = 0.25
        //}

        return cell
    }
    
    return nil
    
    }
    
    return nil
    
  }   
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...