Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "AppDelegate.h"
- #import "SectionCellView.h"
- @interface Person : NSObject
- @end
- @interface Person ()
- @property (nonatomic, retain) NSString *name;
- @end
- @implementation Person
- - (instancetype)initWithName:(NSString *)name {
- self = [super init];
- if (self) {
- _name = name;
- }
- return self;
- }
- @end
- @interface AppDelegate ()
- @property (weak) IBOutlet NSWindow *window;
- @property (nonatomic, strong) IBOutlet NSTableView *tableView;
- @property (nonatomic, strong) NSMutableArray *tableViewContent;
- @end
- @implementation AppDelegate
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
- // Insert code here to initialize your application
- self.tableViewContent = [NSMutableArray arrayWithArray:@[@{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"], @{@"name" : @"person1"}, [[Person alloc] initWithName:@"Peter4"], @{@"name" : @"person2"}, [[Person alloc] initWithName:@"Peter"], @{@"name" : @"person3"}, [[Person alloc] initWithName:@"Peter5"], @{@"name" : @"person4"}, [[Person alloc] initWithName:@"Peter2"], @{@"name" : @"person5"}, [[Person alloc] initWithName:@"Peter3"]]];
- [self.tableView reloadData];
- }
- - (void)applicationWillTerminate:(NSNotification *)aNotification {
- // Insert code here to tear down your application
- }
- #pragma mark - NSTableView
- #pragma mark Data Source
- - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
- return self.tableViewContent.count;
- }
- - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
- return [self.tableViewContent objectAtIndex:row];
- }
- #pragma mark Delegate
- - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
- return [[NSTableRowView alloc] init];
- }
- - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
- NSView *view = [tableView makeViewWithIdentifier:@"CellView" owner:[tableView delegate]];
- if ([self isSectionRow:row]) {
- view = [tableView makeViewWithIdentifier:@"SectionCellView" owner:[tableView delegate]];
- [[[(SectionCellView *)view collapseButton] cell] setRepresentedObject:[NSString stringWithFormat:@"%lu", row]];
- }
- return view;
- }
- - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
- return [self isSectionRow:row] ? 45 : 120;
- }
- - (BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row {
- return [self isSectionRow:row];
- }
- - (BOOL)isSectionRow:(NSInteger)row {
- id object = [self.tableViewContent objectAtIndex:row];
- if (![object isKindOfClass:[Person class]]) {
- return YES;
- }
- return NO;
- }
- - (IBAction)collapse:(id)sender {
- NSButton *button = sender;
- NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[button.cell.representedObject integerValue]+1];
- if (button.state == NSOnState) {
- [self.tableView hideRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationSlideUp];
- } else {
- [self.tableView unhideRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationSlideDown];
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement