有没有办法在Swift中以编程方式设置NSCollectionView?

ste*_*CDP 7 macos cocoa cocoa-bindings nscollectionview swift

我来自iOS开发,我想知道是否有一种方法可以在iOS中以NSCollectionView编程方式设置UICollectionView?并添加NSCollectionViewItems代码.或者是设置NSCollectionView使用绑定的唯一方法?

谢谢!

ste*_*CDP 6

感谢@stevesliva指点我 这个答案.我将它转换为Swift.这就是我得到的.

我在ViewController中创建一个NSCollectionView:

import Cocoa

class ViewController: NSViewController {

var titles = [String]()
var collectionView: NSCollectionView?

override func viewDidLoad() {
    super.viewDidLoad()

    self.titles = ["Banana", "Apple", "Strawberry", "Cherry", "Pear", "Pineapple", "Grape", "Melon"]
    collectionView = NSCollectionView(frame: self.view.frame)
    collectionView!.itemPrototype = CollectionViewItem()
    collectionView!.content = self.titles
    collectionView!.autoresizingMask = NSAutoresizingMaskOptions.ViewWidthSizable | NSAutoresizingMaskOptions.ViewMaxXMargin | NSAutoresizingMaskOptions.ViewMinYMargin | NSAutoresizingMaskOptions.ViewHeightSizable | NSAutoresizingMaskOptions.ViewMaxYMargin

    var index = 0
    for title in titles {
        var item = self.collectionView!.itemAtIndex(index) as! CollectionViewItem
        item.getView().button?.title = self.titles[index]
        index++
    }
    self.view.addSubview(collectionView!)

}
}
Run Code Online (Sandbox Code Playgroud)

ViewController中创建的CollectionViewItem只加载一个视图,我在其中设置项目视图本身.

import Cocoa

class CollectionViewItem: NSCollectionViewItem {

var itemView: ItemView?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
}

override func loadView() {
    self.itemView = ItemView(frame: NSZeroRect)
    self.view = self.itemView!
}

func getView() -> ItemView {
    return self.itemView!
}
}
Run Code Online (Sandbox Code Playgroud)

视图本身:

import Cocoa

class ItemView: NSView {

let buttonSize: NSSize = NSSize(width: 100, height: 20)
let itemSize: NSSize = NSSize(width: 120, height: 40)
let buttonOrigin: NSPoint = NSPoint(x: 10, y: 10)

var button: NSButton?

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)

    // Drawing code here.
}

override init(frame frameRect: NSRect) {
    super.init(frame: NSRect(origin: frameRect.origin, size: itemSize))
    let newButton = NSButton(frame: NSRect(origin: buttonOrigin, size: buttonSize))
    newButton.bezelStyle = NSBezelStyle.RoundedBezelStyle
    self.addSubview(newButton)
    self.button = newButton;
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setButtonTitle(title: String) {
    self.button!.title = title
}
}
Run Code Online (Sandbox Code Playgroud)

要设置按钮标题,我使用的是一种黑客攻击.(ViewController中的for循环)如果有更好的方法来设置标题,请随时发表评论.


Swi*_*oda 5

对于 Swift 2.0,您需要将该collectionView!.autoresizingMask行更改为:

长手:

collectionView?.autoresizingMask = NSAutoresizingMaskOptions([NSAutoresizingMaskOptions.ViewWidthSizable,NSAutoresizingMaskOptions.ViewMaxXMargin,NSAutoresizingMaskOptions.ViewMinYMargin,NSAutoresizingMaskOptions.ViewHeightSizable,NSAutoresizingMaskOptions.ViewMaxYMargin])
Run Code Online (Sandbox Code Playgroud)

或简写:

collectionView?.autoresizingMask = NSAutoresizingMaskOptions([.ViewWidthSizable,.ViewMaxXMargin,.ViewMinYMargin,.ViewHeightSizable,.ViewMaxYMargin])
Run Code Online (Sandbox Code Playgroud)