【问题标题】:Is there a way to set up a NSCollectionView programmatically in Swift?有没有办法在 Swift 中以编程方式设置 NSCollectionView?
【发布时间】:2015-05-05 19:31:50
【问题描述】:

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

谢谢!

【问题讨论】:

  • 要回答最后一个问题,不,您不需要使用绑定。没有需要使用绑定的视图或控件。
  • 但是没有绑定怎么办呢?
  • this转换成swift。
  • 然而,在某些情况下您需要使用绑定,即在设计时还不知道对象的类和/或值时。对 NSCollectionViewItem 实例至关重要的代表对象的绑定就是一个很好的例子。

标签: macos cocoa swift cocoa-bindings nscollectionview


【解决方案1】:

感谢@stevesliva 将我指向this SO answer。我将其转换为 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!)

}
}

在 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!
}
}

视图本身:

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
}
}

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

【讨论】:

  • 设置按钮标题:在 ViewController 的 for 循环中使用:'item.representedObject = title'。然后,在 ItemView: 'NSButton *aButton = [[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20)]; [aButton bind:NSTitleBinding toObject:self withKeyPath:@"representedObject" options:@{NSNullPlaceholderBindingOption:@"No title"}]; [self.view addSubview:aButton];'不要使用“button.title =代表对象”,因为当时不知道代表对象。绑定将在运行时解析。
【解决方案2】:

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

长手:

collectionView?.autoresizingMask = NSAutoresizingMaskOptions([NSAutoresizingMaskOptions.ViewWidthSizable,NSAutoresizingMaskOptions.ViewMaxXMargin,NSAutoresizingMaskOptions.ViewMinYMargin,NSAutoresizingMaskOptions.ViewHeightSizable,NSAutoresizingMaskOptions.ViewMaxYMargin])

或简写:

collectionView?.autoresizingMask = NSAutoresizingMaskOptions([.ViewWidthSizable,.ViewMaxXMargin,.ViewMinYMargin,.ViewHeightSizable,.ViewMaxYMargin])

【讨论】:

    【解决方案3】:

    您需要在NSScrollView 中嵌入NSCollectionView。下面的代码在 Swift 4 中

    设置NSCollectionView

    let layout = NSCollectionViewFlowLayout()
    layout.minimumLineSpacing = 4
    
    collectionView = NSCollectionView()
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.collectionViewLayout = layout
    collectionView.allowsMultipleSelection = false
    collectionView.backgroundColors = [.clear]
    collectionView.isSelectable = true
    collectionView.register(
      Cell.self,
      forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell")
    )
    

    设置NSScrollView

    scrollView = NSScrollView()
    scrollView.documentView = collectionView
    view.addSubview(scrollView)
    

    这是一个简单的NSCollectionViewItem

    class Cell: NSCollectionViewItem {
      let label = NSTextField()
      let imageView = NSImageView()
    
      override func loadView() {
        self.view = NSView()
        self.view.wantsLayer = true
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 2011-03-19
      • 2019-08-03
      • 1970-01-01
      • 2014-07-24
      • 2010-09-14
      相关资源
      最近更新 更多