macoscocoaosx-lionnscollectionviewnscollectionviewitem

how to fix NSCollectionViewItem overlap problem?


i use NSCollectionView to display a horizontal list. but all of the cells overlap in the first cell position, like this.

i don't have enough reputation to post images. [cell1,cell2,cell3], they all overlap in the first cell position. so it look like just a cell, but actually it has 3 cells.

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?


Solution

  • 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. 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];
    }