Swift38/90Days - 用 Swift 開發 Mac App 1 / 3

Swift90Days - 用 Swift 開發 Mac App 1 / 3

今天抽點時間找了篇 Raywenderlich 上的教程入門了一下 Mac App 的開發。git

教程的例子是實現一個簡單的 TableView ,不過在 Mac 裏它叫作 NSTableViewgithub

用法也和 UITableView 類似,經過 delegatedatasource 來加載列表:app

import Cocoa

class MasterViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    var bugs: [ScaryBugDoc]!

    override func viewDidLoad() {
        super.viewDidLoad()

        // init bugs
        var doc1 = ScaryBugDoc(title: "Potato Bug", rating: 4, thumbImage:NSImage(named:"potatoBugThumb")!, fullImage: NSImage(named:"potatoBug")!)
        var doc2 = ScaryBugDoc(title: "House Centipede", rating: 4, thumbImage:NSImage(named:"centipedeThumb")!, fullImage: NSImage(named:"centipede")!)
        var doc3 = ScaryBugDoc(title: "Wolf Spider", rating: 4, thumbImage:NSImage(named:"wolfSpiderThumb")!, fullImage: NSImage(named:"wolfSpider")!)
        var doc4 = ScaryBugDoc(title: "Lady Bug", rating: 4, thumbImage:NSImage(named:"ladybugThumb")!, fullImage: NSImage(named:"ladybug")!)
        bugs = [doc1,doc2,doc3,doc4]
    }



    // MARK: NSTableViewDataSource
    func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView! {
        var cellView = tableView.makeViewWithIdentifier(tableColumn.identifier, owner: self) as NSTableCellView
        if cellView.identifier == "BugColumn" {
            var doc = bugs[row]
            cellView.imageView?.image = doc.thumbImage
            cellView.textField?.stringValue = doc.data.title
        }
        return cellView;
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return bugs.count
    }

}

好吧就這麼點了,感興趣的同窗能夠看下文末的教程連接。ide


原文連接:code

相關文章
相關標籤/搜索