滚动时 NSCollectionViewItems 不重新加载 <OSX - Objective C>

NSCollectionViewItems don't reload when scrolling <OSX - Objective C>

我刚开始学习 Objective-C,我遇到了 NSCollectionView 的问题,我找不到任何解决方法。

我的问题是:

我写了一个按钮事件来创建一个名为 thoughtCollectionViewNSCollectionView 并添加到我的 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

我发现 CollectionItem 有什么问题。 我必须在 ThoughtItem.xib 文件中添加一个 Object,然后将 object class 名称更改为 "ThoughtItem".

然后我将 -(void)viewDidMoveToWindow 更改为可以从 MainViewController 调用的方法。 (或者在MainViewController中创建NSCollectionView也是一种解决方法)

那么问题就迎刃而解了!