スクロールするときにNSCollectionViewItemsがリロードされないObjective-Cを習得するのが初めてで、解決する方法が見つからないNSCollectionView
の問題があります。<OSX - Objective C>
私の問題は、次のとおりです。
私はthoughtCollectionView
という名前NSCollectionView
を作成するために、ボタンイベントを書き、NSCollectionViewItems
を表示するには、私のcontentView
に追加しました。
私がシミュレータを実行すると、項目は表示されますが、スクロールするとリロードされません。
ここに私のコードです:
が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のコードは含まれていませんでした。質問を編集して追加できますか?私はそれが問題の一部かもしれないと思う。 – Nef10
ありがとう!私はちょうど私のThoughtItemsコードを追加しました –
実際にはコレクションビューだけを表示するアプリケーションを実行させるためのコードがいくつか追加されていません。しかし、なぜスクロールビューにコレクションビューを埋め込むのですか? CollectionView自体がスクロールしてはいけませんか?これは、コレクションビュー内をスクロールせず、単にコレクションビューを含むビューをスクロールしないという問題でしょうか?コレクションビューのフレームがスクロール後のビューデバッガをチェックインできますか? – Nef10