2015-05-05 20 views
6

Tôi đến từ sự phát triển iOS và tôi tự hỏi liệu có cách nào để thiết lập một NSCollectionView lập trình như một số UICollectionView trong iOS không? Và thêm mã số NSCollectionViewItems. Hoặc là cách duy nhất để thiết lập một NSCollectionView để sử dụng các ràng buộc?Có cách nào để thiết lập NSCollectionView theo lập trình trong Swift không?

Cảm ơn bạn!

+0

Để trả lời câu hỏi cuối cùng, không, bạn không cần để sử dụng các ràng buộc. Không có chế độ xem hoặc kiểm soát mà bạn cần sử dụng các ràng buộc. – stevesliva

+0

Nhưng làm thế nào tôi có thể làm điều đó mà không có sự ràng buộc? – stefOCDP

+1

Chuyển đổi [this] (http://stackoverflow.com/questions/8660626/how-to-create-nscollectionview-programatically-from-scratch) thành nhanh chóng. – stevesliva

Trả lời

5

Nhờ @stevesliva đã chỉ cho tôi đến this SO answer. Tôi đã chuyển nó sang Swift. Đây là những gì tôi nhận được.

tôi đang tạo ra một NSCollectionView trong 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!) 

} 
} 

Các tạo CollectionViewItem trong ViewController chỉ cần tải một cái nhìn, nơi tôi thiết lập quan điểm mục riêng của mình.

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

Quan điểm riêng của mình:

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

Để đặt tiêu đề nút, tôi đang sử dụng loại một hack. (for-loop trong ViewController) Nếu có cách nào tốt hơn để đặt tiêu đề, vui lòng để lại nhận xét.

+0

Để đặt tiêu đề nút: Trong vòng lặp for của việc sử dụng ViewController: 'item.representedObject = title'. Sau đó, trong ItemView: 'NSButton * aButton = [[Cấp phát NSButton] initWithFrame: NSMakeRect (10, 10, 100, 20)]; [aButton bind: NSTitleBinding toObject: self withKeyPath: @ "representObject" tùy chọn: @ {NSNullPlaceholderBindingOption: @ "No title"}]; [self.view addSubview: aButton]; ' Không sử dụng 'button.title = representObject' vì tệp representObject sẽ không được biết tại thời điểm đó.Các ràng buộc sẽ giải quyết tại thời gian chạy. –

4

Đối Swift 2.0, bạn sẽ cần phải thay đổi dòng collectionView!.autoresizingMask tới:

tay dài:

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

hay tay ngắn:

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

Bạn cần phải nhúng NSCollectionView trong NSScrollView . Mã dưới đây là trong Swift 4

Cài đặt 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") 
) 

Cài đặt NSScrollView

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

Dưới đây là một đơn giản NSCollectionViewItem

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

    override func loadView() { 
    self.view = NSView() 
    self.view.wantsLayer = true 
    } 
} 
Các vấn đề liên quan