有没有办法在Swift中以编程方式设置NSCollectionView? [英] Is there a way to set up a NSCollectionView programmatically in Swift?
问题描述
我来自iOS开发,我想知道是否有一个方法来设置一个NSCollectionView的程序化的像UICollectionView在iOS中?并在代码中添加NSCollectionViewItems。或者是设置NSCollectionView以使用绑定的唯一方法?
谢谢!
感谢@stevesliva指向我这个SO回答。我把它转换为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
标题中的标题{
var item = self.collectionView!.itemAtIndex(index)as! CollectionViewItem
item.getView()。button?.title = self.titles [index]
index ++
}
self.view.addSubview(collectionView!)
$在ViewController中创建的CollectionViewItem只是加载一个视图,其中bView
<我设置了项目视图本身。 import Cocoa
类CollectionViewItem:NSCollectionViewItem {
var itemView:ItemView?
override func viewDidLoad(){
super.viewDidLoad()
//这里做视图设置。
}
override func loadView(){
self.itemView = ItemView(frame:NSZeroRect)
self.view = self.itemView!
}
func getView() - > ItemView {
return self.itemView!
}
}
视图本身:
import Cocoa
class ItemView:NSView {
let buttonSize:NSSize = NSSize ,height:20)
let itemSize:NSSize = NSSize(width:120,height:40)
let buttonOrigin:NSPoint = NSPoint(x:10,y:10)
$ b b var button:NSButton?
override func drawRect(dirtyRect:NSRect){
super.drawRect(dirtyRect)
//这里绘制代码。
}
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;
}
需要init?(coder:NSCoder){
fatalError(init(coder :) has not been implemented)
}
b $ b func setButtonTitle(title:String){
self.button!.title = title
}
}
要设置按钮标题,我使用的是一种黑客。 (ViewController中的for循环)如果有更好的方法设置标题,请随意留下评论。
I'm coming from the iOS development and I am wondering if there is a way to set up a NSCollectionView programmatically like a UICollectionView in iOS? And add the NSCollectionViewItems in code. Or is the only way to set up a NSCollectionView to use bindings?
Thank You!
解决方案 Thanks to @stevesliva for pointing me to this SO answer. I converted it to Swift.
This is what I got.
I am creating a NSCollectionView in the ViewController:
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!)
}
}
The created CollectionViewItem in the ViewController just load a view, where I set up the item view itself.
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!
}
}
The view itself:
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
}
}
To set the button title, I am using kind of a hack. (the for-loop in the ViewController) If there is a better way to set the title please feel free to leave a comment.
这篇关于有没有办法在Swift中以编程方式设置NSCollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!