1

을 내가 목표 - C를 배우고 새로운 그리고 난 내가 그것을 해결하기 위해 어떤 방법을 찾을 수 없습니다 NSCollectionView에 문제가 있습니다.NSCollectionViewItems는 다시로드하지 않습니다 <OSX를 - 목적 C>

내 문제는 다음과 같습니다

은 내가 NSCollectionView 이름 thoughtCollectionView을 만들기 위해 버튼 이벤트를 쓴 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 
+0

코드에 명백한 오류가 표시되지 않습니다. 그래서 나는 그것을 실행하려고했지만 당신이 주목 한 것보다'ThoughtItems '에 대한 코드를 포함하지 않았다. 질문을 수정하고 추가 할 수 있습니까? 나는 그것이 그것이 문제의 일부라고 생각한다. – Nef10

+0

감사합니다.방금 ThoughtItems 코드를 추가했습니다. –

+0

코드가 컬렉션 뷰를 표시하는 앱을 실제로 실행하는 데 필요한 정보가 더 이상 없습니다. 그런데 왜 컬렉션 뷰를 스크롤 뷰에 포함합니까? CollectionView 자체가 스크롤해야하지 않습니까? 이것이 콜렉션 뷰 내에서 스크롤하지 않고 대신 콜렉션 뷰만을 포함하는 뷰를 스크롤하는 문제 일 수 있습니까? 스크롤 한 후에 콜렉션 뷰의 프레임이있는 뷰 디버거를 체크인 할 수 있습니까? – Nef10

답변

0

나는 CollectionItem의 문제 무엇 것을 발견했다. 그때 "ThoughtItem"object 클래스 이름을 변경 ThoughtItem.xib 파일에 Object을 추가해야합니다.

는 그리고 나는 MainViewController에서 호출 할 수있는 방법에 -(void)viewDidMoveToWindow을 변경합니다. (또는 MainViewController에서 NSCollectionView을 작성하는 것도 문제를 해결하는 방법입니다)

그러면 모든 문제가 해결됩니다!