Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

objective c - NSCollectionView does not scroll items past initial visible rect

I'm running into an issue with an existing app while trying to fix a few outstanding macOS 10.13 bugs. I have a small NSCollectionView that looks similar to the small monthly calendar in the Calendar app. It displays days and scrolls vertically on click. However, on macOS 10.13 the collection view only displays a few rows and does not scroll. I've verified that the data source is being called correctly and it does try to load additional items - but doesn't scroll to them.

I've created a small sample application that also demonstrates the issue. This is a basic macOS app that adds an NSCollectionView via the Main storyboard and has a generic NSCollectionViewItem class loaded from a nib. The entirety of the code in the main view controller is:

- (void)viewDidLoad {  
  [super viewDidLoad];  

  NSNib *nib = [[NSNib alloc] initWithNibNamed:@"FooCollectionViewItem" bundle:nil];  
  [self.collectionView registerNib:nib forItemWithIdentifier:@"foo"];  
}  
- (void)viewDidAppear {  
  [super viewDidAppear];  

  [self.collectionView reloadData];  
}  
- (void)setRepresentedObject:(id)representedObject {  
  [super setRepresentedObject:representedObject];  

}  
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {  
  return 1;  
}  
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {  
  return 2000;  
}  
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {  
  FooCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"foo" forIndexPath:indexPath];  
  item.fooField.stringValue = [NSString stringWithFormat:@"%ld", indexPath.item];  

  return item;  
}

The resulting app looks like:

enter image description here

Unfortunately, that's the entirely of the collection view. Scrolling doesn't scroll through any additional items. I've uploaded the sample app to https://www.dropbox.com/sh/wp2y7g0suemzcs1/AABVKfTZq54J7riy6BR7Mhxha?dl=0 in case that's useful. Any ideas why this is breaking on 10.13 for me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar issue with NSCollectionView scroll running High Sierra. Here's how I solved it:

SWIFT

  if #available(OSX 10.13, *) {
    if let contentSize = self.collectionView.collectionViewLayout?.collectionViewContentSize {
      self.collectionView.setFrameSize(contentSize)
    }
  }

OBJ-C

  if (@available(macOS 10.13, *)) {
    [self.collectionView setFrameSize: self.collectionView.collectionViewLayout.collectionViewContentSize];
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...