我正在使用nsCollectionView,CollectionView有标题。我需要为每个标题设置一个特定的标题。
我的代码:
func collectionView(collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> NSView {
var view: NSView?
if kind == NSCollectionElementKindSectionHeader {
view = collectionView.makeSupplementaryViewOfKind(kind, withIdentifier: "Header", forIndexPath: indexPath)
}
...
return view!
}
Header
是一个NSCollectionViewItem
import Cocoa
class Header: NSCollectionViewItem {
var title: String!
...
//Write title value in a textField
...
}
我的问题是:如何从
viewForSupplementaryElementOfKind
设置标题值?我需要这样的东西:
最佳答案
这里有点棘手,我希望苹果能通过更好地利用NSCollectionViewItem
实际上,从示例代码CocoaSlideCollection
中,使用可变文本显示标题的方法是通过查看头视图的子视图并获取对NSTextField
的引用,然后设置stringValue
。
与斯威夫特合作:
创建一个HeaderView
它是nsview的一个子类
将HeaderView
设置为页眉笔尖视图
在HeaderView
中,实现此变量titleTextField
lazy var titleTextField: NSTextField? = {
for view in self.subviews {
if view is NSTextField {
return view as? NSTextField
}
}
return nil
}()
在
viewForSupplementaryElementOfKind
委托方法中,执行以下操作let view = collectionView.makeSupplementaryViewOfKind(kind, withIdentifier: nibName!, forIndexPath: indexPath)
if let view = view as? HeaderView {
view.titleTextField?.stringValue = "Header Custom Value"
}
return view
关于swift - 使用NSCollectionView设置标题标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34802573/