Advertisement
Guest User

NSTableView unhideRowsAtIndexes crashes

a guest
Jun 3rd, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import "AppDelegate.h"
  3.  
  4. #import "SectionCellView.h"
  5.  
  6. @interface Person : NSObject
  7.  
  8. @end
  9.  
  10. @interface Person ()
  11.  
  12. @property (nonatomic, retain) NSString *name;
  13. @end
  14.  
  15. @implementation Person
  16.  
  17. - (instancetype)initWithName:(NSString *)name {
  18.     self = [super init];
  19.     if (self) {
  20.         _name = name;
  21.     }
  22.     return self;
  23. }
  24.  
  25. @end
  26.  
  27. @interface AppDelegate ()
  28.  
  29. @property (weak) IBOutlet NSWindow *window;
  30.  
  31. @property (nonatomic, strong) IBOutlet NSTableView *tableView;
  32. @property (nonatomic, strong) NSMutableArray *tableViewContent;
  33. @end
  34.  
  35. @implementation AppDelegate
  36.  
  37. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  38.     // Insert code here to initialize your application
  39.    
  40.     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"]]];
  41.    
  42.     [self.tableView reloadData];
  43. }
  44.  
  45. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  46.     // Insert code here to tear down your application
  47. }
  48.  
  49.  
  50.  
  51. #pragma mark - NSTableView
  52.  
  53. #pragma mark Data Source
  54.  
  55. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
  56.     return self.tableViewContent.count;
  57. }
  58.  
  59. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
  60.     return [self.tableViewContent objectAtIndex:row];
  61. }
  62.  
  63. #pragma mark Delegate
  64.  
  65. - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
  66.     return [[NSTableRowView alloc] init];
  67. }
  68.  
  69. - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
  70.    
  71.     NSView *view = [tableView makeViewWithIdentifier:@"CellView" owner:[tableView delegate]];
  72.    
  73.     if ([self isSectionRow:row]) {
  74.        
  75.         view = [tableView makeViewWithIdentifier:@"SectionCellView" owner:[tableView delegate]];
  76.         [[[(SectionCellView *)view collapseButton] cell] setRepresentedObject:[NSString stringWithFormat:@"%lu", row]];
  77.     }
  78.    
  79.     return view;
  80. }
  81.  
  82. - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
  83.     return [self isSectionRow:row] ? 45 : 120;
  84. }
  85.  
  86. - (BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row {
  87.     return [self isSectionRow:row];
  88. }
  89.  
  90. - (BOOL)isSectionRow:(NSInteger)row {
  91.    
  92.     id object = [self.tableViewContent objectAtIndex:row];
  93.    
  94.     if (![object isKindOfClass:[Person class]]) {
  95.         return YES;
  96.     }
  97.    
  98.     return NO;
  99. }
  100.  
  101. - (IBAction)collapse:(id)sender {
  102.    
  103.     NSButton *button = sender;
  104.    
  105.     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[button.cell.representedObject integerValue]+1];
  106.    
  107.     if (button.state == NSOnState) {
  108.         [self.tableView hideRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationSlideUp];
  109.     } else {
  110.         [self.tableView unhideRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationSlideDown];
  111.     }
  112. }
  113.  
  114. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement