【发布时间】:2017-09-30 18:28:29
【问题描述】:
我刚开始学习 Objective-C,我遇到了 NSCollectionView 的问题,我找不到任何解决方法。
我的问题是:
我写了一个按钮事件来创建一个名为thoughtCollectionView 的NSCollectionView 并添加到我的contentView 以显示NSCollectionViewItems。
当我运行模拟器时,项目看起来还不错,但是当我滚动时它们不会重新加载。
这是我的代码:
MainViewController.m
- (void)thoughtShow{
// Add Collection View
thoughtCollectionView = [[ThoughtCollection alloc] initWithFrame:NSMakeRect(0, 0, contentWidth, contentHeight)];
NSCollectionViewFlowLayout *layout = [[NSCollectionViewFlowLayout alloc] init];
[thoughtCollectionView setCollectionViewLayout:layout];
[layout setScrollDirection:NSCollectionViewScrollDirectionVertical];
layout.itemSize = NSMakeSize(50, 50);
layout.minimumInteritemSpacing = 20;
layout.sectionInset = NSEdgeInsetsMake(20, 20, 20, 20);
[thoughtCollectionView registerClass:ThoughtItems.self forItemWithIdentifier:@"ThoughtItems"];
thoughtScrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, contentWidth, contentHeight)];
thoughtScrollView.documentView = thoughtCollectionView;
[contentView addSubview:thoughtScrollView];
[thoughtCollectionView reloadData];
[thoughtCollectionView setNeedsDisplay:YES];
[thoughtCollectionView layoutSubtreeIfNeeded];
}
MainViewController.h
@interface MainViewController : NSViewController <NSCollectionViewDelegateFlowLayout, NSCollectionViewDelegate, NSCollectionViewDataSource>
@end
ThoughtCollectionView.h
@interface ThoughtCollection : NSCollectionView <NSCollectionViewDataSource, NSCollectionViewDelegate, NSCollectionViewDelegateFlowLayout>
{
NSMutableArray * ar;
}
@end
ThoughtCollectionView.m
@implementation ThoughtCollection
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath{
ThoughtItems *item = [collectionView makeItemWithIdentifier:@"ThoughtItems" forIndexPath:indexPath];
NSLog(@"Reloading item");
return item;
}
- (void)collectionView:(NSCollectionView *)collectionView willDisplayItem:(nonnull NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath{
}
- (void)collectionView:(NSCollectionView *)collectionView didEndDisplayingItem:(nonnull NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath{
}
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"Collection view count : %lu", [ar count]);
return [ar count];
}
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView{
return 1;
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(NSRect)newBounds
{
return YES;
}
- (void)viewDidMoveToWindow {
NSLog(@"My collection View");
self.delegate = self;
self.dataSource = self;
ar = [[NSMutableArray alloc] init];
for (int n = 0; n < 1000; n++) {
[ar addObject:@"Hello"];
}
[self reloadData];
[self setNeedsDisplay:YES];
[self layoutSubtreeIfNeeded];
}
@end
模拟器显示: enter image description here
滚动: enter image description here
ThoughtItem.m
#import "ThoughtItems.h"
@interface ThoughtItems ()
@end
@implementation ThoughtItems
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setWantsLayer:YES];
[self.view.layer setBackgroundColor:[[NSColor orangeColor] CGColor]];
}
@end
ThoughtItem.h
#import <Cocoa/Cocoa.h>
@interface ThoughtItems : NSCollectionViewItem
@end
【问题讨论】:
-
我在您的代码中没有看到明显的错误。因此我尝试运行它,但注意到您没有包含
ThoughtItems的代码。您可以编辑您的问题并添加它吗?我认为这可能是问题的一部分。 -
谢谢!我刚刚添加了我的 ThoughtItems 代码
-
代码缺少更多的东西来实际运行一个只显示集合视图的应用程序。但是为什么要将集合视图嵌入到滚动视图中呢? CollectionView 不应该自己滚动吗?这可能是您没有在集合视图中滚动而是滚动只包含集合视图的视图的问题吗?您可以在视图调试器中检查集合视图的框架在滚动后的位置吗?
-
请问缺少的代码是什么? >
-
感谢您的帮助!我发现了问题!
标签: objective-c macos macos-sierra nscollectionview nscollectionviewitem