2015-05-05 24 views
6

Przychodzę z rozwoju iOS i zastanawiam się, czy jest jakiś sposób programowo ustawić NSCollectionView jak UICollectionView w systemie iOS? I dodaj kod NSCollectionViewItems. Czy jest to jedyny sposób, aby skonfigurować NSCollectionView do korzystania z powiązań?Czy istnieje sposób programistycznego skonfigurowania NSCollectionView w Swift?

Dziękujemy!

+0

Aby odpowiedzieć na ostatnie pytanie, nie, nie trzeba używać wiązań. Nie ma widoku ani kontroli, dla której musisz użyć powiązań. – stevesliva

+0

Ale jak mogę to zrobić bez wiązań? – stefOCDP

+1

Konwersja [this] (http://stackoverflow.com/questions/8660626/how-to-create-nscollectionview-programatically-from-scratch) na szybki. – stevesliva

Odpowiedz

5

Dzięki @stevesliva za skierowanie mnie do this SO answer. Przekonałem go do Swifta. Oto, co mam.

Tworzę NSCollectionView w 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!) 

} 
} 

Utworzona CollectionViewItem w ViewController prostu wczytać widoku, gdzie założyłem sam widok elementu.

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

Sam widok:

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

Aby ustawić tytuł przycisk używam rodzaj hack. (pętla for w ViewController) Jeśli istnieje lepszy sposób na ustawienie tytułu, zostaw komentarz.

+0

Aby ustawić tytuł przycisku: W pętli for narzędzia ViewController użyj: 'item.representedObject = title'. Następnie w ItemView: "NSButton * aButton = [[przydział NSButton] initWithFrame: NSMakeRect (10, 10, 100, 20)]; [aButton bind: NSTitleBinding toObject: self withKeyPath: @ "representObject" opcje: @ {NSNullPlaceholderBindingOption: @ "Brak tytułu"}]; [self.view addSubview: aButton]; ' Nie używaj "button.title = representObject", ponieważ obiekt representObject nie będzie znany w tym czasie.Powiązanie zostanie rozwiązane w czasie wykonywania. –

4

Dla Swift 2.0, trzeba będzie zmienić linię collectionView!.autoresizingMask do:

długo ręka:

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

lub krótki ręka:

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

trzeba osadzić NSCollectionView ciągu NSScrollView . Poniższy kod jest Swift 4

Konfiguracja 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") 
) 

Konfiguracja NSScrollView

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

Oto prosty NSCollectionViewItem

class Cell: NSCollectionViewItem { 
    let label = NSTextField() 
    let imageView = NSImageView() 

    override func loadView() { 
    self.view = NSView() 
    self.view.wantsLayer = true 
    } 
} 
Powiązane problemy