1

我是学习 Objective-C 的新手,我有一个问题NSCollectionView,我找不到任何方法来解决它。

我的问题是:

我写了一个按钮事件来创建一个NSCollectionView命名thoughtCollectionView并添加到我contentView的 to display NSCollectionViewItems

当我运行模拟器时,项目看起来不错,但是当我滚动时它们不会重新加载。

这是我的代码:

主视图控制器.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];
}

主视图控制器.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

模拟器显示: 在此处输入图像描述

滚动: 在此处输入图像描述

思想项目.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

思想项.h

#import <Cocoa/Cocoa.h>

@interface ThoughtItems : NSCollectionViewItem

@end
4

1 回答 1

0

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

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

那么所有的问题都解决了!

于 2017-05-03T09:03:13.390 回答