滚动时,NSCollectionViewItems不会重新加载<osx - =“”objective =“”c =“”>

时间:2017-05-03 04:50:16

标签: objective-c macos macos-sierra nscollectionview nscollectionviewitem

我是初次学习Objective-C的人,我遇到了NSCollectionView的问题,我找不到任何方法来解决它。

我的问题是:

我写了一个按钮事件来创建名为NSCollectionView的{​​{1}}并添加到我的thoughtCollectionView上以显示contentView

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

这是我的代码:

MainViewController.m

NSCollectionViewItems

MainViewController.h

- (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];
}

ThoughtCollectionView.h

@interface MainViewController : NSViewController <NSCollectionViewDelegateFlowLayout, NSCollectionViewDelegate, NSCollectionViewDataSource>
@end

ThoughtCollectionView.m

@interface ThoughtCollection : NSCollectionView <NSCollectionViewDataSource, NSCollectionViewDelegate, NSCollectionViewDelegateFlowLayout>
{
    NSMutableArray * ar;
}

@end

模拟器显示: enter image description here

滚动: enter image description here

  

ThoughtItem.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
  

ThoughtItem.h

#import "ThoughtItems.h"

@interface ThoughtItems ()

@end

@implementation ThoughtItems

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setWantsLayer:YES];
    [self.view.layer setBackgroundColor:[[NSColor orangeColor] CGColor]];
}

@end

1 个答案:

答案 0 :(得分:0)

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

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

然后所有问题都解决了!