简体   繁体   English

如何解决 NSCollectionViewItem 重叠问题?

[英]how to fix NSCollectionViewItem overlap problem?

i use NSCollectionView to display a horizontal list.我使用 NSCollectionView 来显示水平列表。 but all of the cells overlap in the first cell position, like this.但所有单元格在第一个单元格 position 中重叠,就像这样。

i don't have enough reputation to post images.我没有足够的声誉来发布图片。 [cell1,cell2,cell3], they all overlap in the first cell position. [cell1,cell2,cell3],它们都在第一个单元格 position 中重叠。 so it look like just a cell, but actually it has 3 cells.所以它看起来只是一个单元格,但实际上它有 3 个单元格。

my main code:我的主要代码:

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

How can i do something to fix it?我该怎么做才能解决它?

I have found the reason.我找到了原因。 The problem is in my custom cell.问题出在我的自定义单元格中。

@interface ScreenShotCellView ()

@property (weak) IBOutlet NSView *containerView;

@end

@implementation ScreenShotCellView

- (void)loadView {
    self.view = self.containerView;
}

@end

in the loadView method.loadView方法中。 self.view = self.containerView; make always display in the first cell.始终显示在第一个单元格中。

after changed like this, it becomes normal.改成这样后就正常了。

- (void)loadView {
    self.view = [[NSView alloc] init];
    [self.view addSubview:self.containerView];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM