macOS NSCollectionView简单操作

先看效果

最简单的两份文件,复制-生产即可运行

第一份(主要)

ViewController.h

#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController

@end

ViewController.m

@interface ViewController ()<NSCollectionViewDelegate,NSCollectionViewDataSource>

//具体层级关系是NSScrollView——NSClipView——NSCollectionView。

/** 滚动显示 */
@property (nonatomic, strong) NSScrollView *scrollView;
//NSCollectionView显示
@property (nonatomic, strong) NSCollectionView *collectionView;
//数据项(
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation ViewController

#pragma mark 懒加载 macos
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithArray:@[@"张三", @"李四", @"王五"]];
    }
    return _dataSource;
}

- (NSCollectionView*)collectionView{
    if(!_collectionView){
        _collectionView = [[NSCollectionView alloc] init];
        [self.scrollView setDocumentView:self.collectionView];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.selectable = YES;
        _collectionView.backgroundColors = @[[NSColor colorWithCalibratedRed:5.0 green:0.0 blue:255.0 alpha:1.0]];
        [_collectionView registerClass:FSitem.class forItemWithIdentifier:@"FSitem"];
        
    }
    return _collectionView;
}

- (NSScrollView*)scrollView{
    if(!_scrollView){
        _scrollView = [[NSScrollView alloc]init];
        [self.view addSubview:self.scrollView];
       [_scrollView setBorderType:NSNoBorder];
       [_scrollView setHasVerticalScroller:NO];//垂直
       [_scrollView setHasHorizontalScroller:YES];//水平
       [_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

    }
    return _scrollView;
}
//自适应
- (void)configureCollectionViewLayout{
    NSCollectionViewFlowLayout *flowLayout = [[NSCollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.itemSize = NSMakeSize(30, 30);
    flowLayout.scrollDirection = NSCollectionViewScrollDirectionHorizontal;
    
    [self.collectionView setCollectionViewLayout:flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.

    [self configureCollectionViewLayout];
    
    /** 用户确认书滚动条 */
    CGFloat tSVY = 200;
    CGFloat tSVW = 50;
    CGFloat tSVH = 20;
    CGFloat tSVX = 20;
    self.scrollView.frame = CGRectMake(tSVX, tSVY, tSVW, tSVH);
    
    CGFloat tVY = 20;
    CGFloat tVW = 200;
    CGFloat tVH = 200;
    CGFloat tVX = 20;
    self.collectionView.frame = CGRectMake(tVX, tVY, tVW, tVH);

    [[self.scrollView contentView] setPostsBoundsChangedNotifications: YES];//发起通知
    //找个合适的地儿,注册通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter] ;
        [center addObserver: self
                   selector: @selector(boundsDidChangeNotification:)
                       name: NSViewBoundsDidChangeNotification
                     object: [self.scrollView contentView]];
    
    
}

- (void) boundsDidChangeNotification: (NSNotification *) notification{
    // 在这里进行处理
    NSClipView *changedContentView = [notification object];
    
    // get the origin of the NSClipView of the scroll view that
    // we're watching
    
    NSPoint changedBoundsOrigin = [changedContentView documentVisibleRect].origin;
    //判断滚动条移动高度变化确认是否移动拉动滚动到底
    NSLog(@"滑动了:%f",changedBoundsOrigin.y);
    if (changedBoundsOrigin.y > 560) {
//        self.argeeButton.enabled = YES;
    }else{
//        self.argeeButton.enabled = NO;
    }
    
}

//竖(列)
-(NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView{
    return self.dataSource.count;
}
//横(行)
-(NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}
//显示出来
-(NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath{
    FSitem *item = [collectionView makeItemWithIdentifier:@"FSitem" forIndexPath:indexPath];
    if (!item) {
        item = [[FSitem alloc] initWithNibName:@"FSitem" bundle:nil];
    }
//    item.name.stringValue = self.dataSource[indexPaths];
    return item;
}

//选中item调用的方法
-(void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths{
    NSLog(@"选中了%@",indexPaths);
}

//实时改变item大小
- (NSSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.item == 0) {
//           NSSize rect = NSMakeSize(collectionView.bounds.size.width - 15, 50);
//           NSLog(@"sizeForItemAtIndexPath 2 %@",NSStringFromSize(rect));
//           return rect;
//       }
//
    return CGSizeMake(100, 60);//宽,高

}

- (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;{
    return NSEdgeInsetsMake(20, 30, 100, 200);//顶,左,下,右

}

FSitem.h(带xib 继承NSCollectionViewItem)

#import <Cocoa/Cocoa.h>


@interface FSitem : NSCollectionViewItem

@property (weak) IBOutlet NSTextField *name;

@end

FSitem.m

#import "FSitem.h"

@interface FSitem ()

@end

@implementation FSitem

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    self.view.wantsLayer = YES;
    self.view.layer.backgroundColor = [NSColor systemBlueColor].CGColor;
    
}

@end

FSitem.xib

 

posted on 2022-10-14 15:31  高彰  阅读(454)  评论(1编辑  收藏  举报

导航