我已经创建了NSCollectionView的子类,它符合NSCollectionViewDataSource,NSCollectionViewDelegate但它每次都挂起 - 我只得到旋转的沙滩球。
public class SequenceCollectionView : NSCollectionView, NSCollectionViewDataSource, NSCollectionViewDelegate {
// MARK: Inits
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
self.dataSource = self
self.delegate = self
}
//MARK: Datasource
public func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
public func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
if indexPath.item == 0 {
let item = NSCollectionViewItem(nibName: "ItemView", bundle: nil)
}
return NSCollectionViewItem()
}
}
显然在某种循环或保留循环中,但我发现很难调试。
答案 0 :(得分:2)
发现问题在于可选的委托调用
public func collectionView(collectionView: NSCollectionView, willDisplayItem item: NSCollectionViewItem, forRepresentedObjectAtIndexPath indexPath: NSIndexPath) {
}
通过将此添加到类已解决了问题。
答案 1 :(得分:0)
添加到Chris'的答案,NSCollectionView将在滚动或调整视图大小时挂起,除非覆盖以下可选委托方法:
- (void)collectionView:(NSCollectionView *)collectionView didEndDisplayingItem:(NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {}