Tôi không chắc có nhiều thông tin chi tiết trong việc tạo chế độ xem bộ sưu tập theo chương trình và không có sự ràng buộc, nhưng ở đây nó đi.
Giới thiệu
cơ bản Có bốn thành phần khi sử dụng một cái nhìn bộ sưu tập:
- Xem: một lớp con của
NSView
, chịu trách nhiệm cho việc hiển thị thông tin;
- Chế độ xem bộ sưu tập;
- Xem bộ điều khiển: một lớp con của
NSCollectionViewItem
đóng vai trò là mẫu thử nghiệm xem bộ sưu tập;
- Mô hình: một mảng các đối tượng.
Thông thường chế độ xem được thiết kế trong Trình tạo giao diện và mô hình được dàn xếp bằng các kết buộc Cocoa.
Làm nó lập trình:
Constants
static const NSSize buttonSize = {80, 20};
static const NSSize itemSize = {100, 40};
static const NSPoint buttonOrigin = {10, 10};
Xem
Đây là một chế độ xem chuẩn (một giao diện tùy chỉnh trong giao diện Builder cách nói) có chứa một nút. Lưu ý rằng chế độ xem có kích thước cố định.
@interface BVView : NSView
@property (weak) NSButton *button;
@end
@implementation BVView
@synthesize button;
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:(NSRect){frameRect.origin, itemSize}];
if (self) {
NSButton *newButton = [[NSButton alloc]
initWithFrame:(NSRect){buttonOrigin, buttonSize}];
[self addSubview:newButton];
self.button = newButton;
}
return self;
}
@end
View Controller (thử nghiệm)
Thông thường một bộ điều khiển xem tải quan điểm của mình từ một tập tin nib. Trong những trường hợp hiếm hoi mà bộ điều khiển chế độ xem không nhận được chế độ xem từ tệp nib, nhà phát triển phải gửi nó theo số -setView:
trước khi -view
nhận được bởi trình điều khiển chế độ xem hoặc ghi đè -loadView
. Các mã sau đây không sau này.
Xem bộ điều khiển nhận đối tượng mô hình tương ứng qua -setRepresentedObject:
. Tôi đã ghi đè nó để cập nhật tiêu đề nút bất cứ khi nào đối tượng mô hình thay đổi. Lưu ý rằng điều này có thể được thực hiện bằng cách sử dụng kết buộc Cocoa mà không có bất kỳ mã nào cả.
Lưu ý rằng không có mã nào cụ thể cho chế độ xem bộ sưu tập - đó là hành vi của trình điều khiển chế độ xem chung.
@interface BVPrototype : NSCollectionViewItem
@end
@implementation BVPrototype
- (void)loadView {
[self setView:[[BVView alloc] initWithFrame:NSZeroRect]];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
[[(BVView *)[self view] button] setTitle:representedObject];
}
@end
Mẫu
Một mảng đơn giản của chuỗi đại diện cho danh hiệu nút:
@property (strong) NSArray *titles;
self.titles = [NSArray arrayWithObjects:@"Case", @"Molly", @"Armitage",
@"Hideo", @"The Finn", @"Maelcum", @"Wintermute", @"Neuromancer", nil];
Collection Xem
Cho đến nay, mối quan hệ duy nhất đó là được thiết lập là quan điểm (BVView
) được sử dụng theo mẫu thử nghiệm (BVPrototype
). Khung nhìn bộ sưu tập phải được thông báo về nguyên mẫu mà nó cần sử dụng cũng như mô hình để lấy dữ liệu từ đó.
NSCollectionView *cv = [[NSCollectionView alloc]
initWithFrame:[[[self window] contentView] frame]];
[cv setItemPrototype:[BVPrototype new]];
[cv setContent:[self titles]];
Full Source Code cho các ứng dụng Đại biểu
#import "BVAppDelegate.h"
static const NSSize buttonSize = { 80, 20 };
static const NSSize itemSize = { 100, 40 };
static const NSPoint buttonOrigin = { 10, 10 };
@interface BVView : NSView
@property (weak) NSButton *button;
@end
@implementation BVView
@synthesize button;
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:(NSRect){frameRect.origin, itemSize}];
if (self) {
NSButton *newButton = [[NSButton alloc]
initWithFrame:(NSRect){buttonOrigin, buttonSize}];
[self addSubview:newButton];
self.button = newButton;
}
return self;
}
@end
@interface BVPrototype : NSCollectionViewItem
@end
@implementation BVPrototype
- (void)loadView {
[self setView:[[BVView alloc] initWithFrame:NSZeroRect]];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
[[(BVView *)[self view] button] setTitle:representedObject];
}
@end
@interface BVAppDelegate()
@property (strong) NSArray *titles;
@end
@implementation BVAppDelegate
@synthesize window = _window;
@synthesize titles;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.titles = [NSArray arrayWithObjects:@"Case", @"Molly", @"Armitage",
@"Hideo", @"The Finn", @"Maelcum", @"Wintermute", @"Neuromancer", nil];
NSCollectionView *cv = [[NSCollectionView alloc]
initWithFrame:[[[self window] contentView] frame]];
[cv setItemPrototype:[BVPrototype new]];
[cv setContent:[self titles]];
[cv setAutoresizingMask:(NSViewMinXMargin
| NSViewWidthSizable
| NSViewMaxXMargin
| NSViewMinYMargin
| NSViewHeightSizable
| NSViewMaxYMargin)];
[[[self window] contentView] addSubview:cv];
}
@end
Tài liệu trên NSCollectionView đáng ngạc nhiên kém. Bất cứ ai hiểu cách con thú thần thoại này hoạt động, và có thể chia sẻ kiến thức của họ, đang giúp đỡ các nhà phát triển Objective-C ở mọi nơi. Cảm ơn nhiều. – Tronathan
Bavarious, tôi có thể yêu cầu bạn hiển thị cách đồng bộ hóa chế độ xem bộ sưu tập với các mục có thể thay đổi không? – brigadir
@Bavarious - cảm ơn vì đã có câu trả lời tuyệt vời. –