[英]how to fix NSCollectionViewItem overlap problem?
我使用 NSCollectionView 來顯示水平列表。 但所有單元格在第一個單元格 position 中重疊,就像這樣。
我沒有足夠的聲譽來發布圖片。 [cell1,cell2,cell3],它們都在第一個單元格 position 中重疊。 所以它看起來只是一個單元格,但實際上它有 3 個單元格。
我的主要代碼:
ViewController.h
- (void)viewDidLoad {
[super viewDidLoad];
self.flowLayout.scrollDirection = NSCollectionViewScrollDirectionHorizontal;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.selectable = YES;
self.collectionView.allowsMultipleSelection = YES;
[self.collectionView registerNib:[[NSNib alloc] initWithNibNamed:@"ScreenShotCellView" bundle:nil] forItemWithIdentifier:NSStringFromClass([ScreenShotCellView class])];
}
#pragma mark - NSCollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {
[self.collectionView.collectionViewLayout invalidateLayout];
return 1;
}
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.screenshotItems.count;
}
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
NSCollectionViewItem *itemCell = [self.collectionView makeItemWithIdentifier:NSStringFromClass([ScreenShotCellView class]) forIndexPath:indexPath];
if ([itemCell isKindOfClass:[ScreenShotCellView class]]) {
ScreenShotCellView *cellView = (ScreenShotCellView *)itemCell;
ScreenshotItem *itemData = [self.screenshotItems objectAtIndex:indexPath.item];
cellView.item = itemData;
}
return itemCell;
}
#pragma mark - NSCollectionViewDelegateFlowLayout
- (NSSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, self.collectionView.frame.size.height - 20);
}
- (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
- (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}
我該怎么做才能解決它?
我找到了原因。 問題出在我的自定義單元格中。
@interface ScreenShotCellView ()
@property (weak) IBOutlet NSView *containerView;
@end
@implementation ScreenShotCellView
- (void)loadView {
self.view = self.containerView;
}
@end
在loadView
方法中。 self.view = self.containerView;
始終顯示在第一個單元格中。
改成這樣后就正常了。
- (void)loadView {
self.view = [[NSView alloc] init];
[self.view addSubview:self.containerView];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.