问题描述:

please forgive me for taking your time, but i spent a lot of time and tried everything that was in my mind and that i found on internet :/

The problem is about reloading NSTableView after adding 1 record to its array. My application starts with scanning directory for files, adding their names and modified dates into NSMuttableArray of NSDictionaries. Then loading this information to NSTableView, and everything works fine.

But when I'am creating new file and trying to reload NStableViev I'am getting this:

[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]

Now i want to show some code of what I'am trying to do.

So here is how my table getting data

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{

NSLog(@"--------->%ld",(long)filesCount);

return filesCount;

}

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn

row:(NSInteger)row {

NSDictionary *flag = FilesArray[row];

NSString *identifier = [tableColumn identifier];

if ([identifier isEqualToString:@"filename"]){

NSTableCellView *cellview = [tableView makeViewWithIdentifier:@"filename" owner:self];

[cellview.textField setStringValue:flag[@"name"]];

return cellview;

}else if ([identifier isEqualToString:@"filedate"]){

NSTableCellView *cellview = [tableView makeViewWithIdentifier:@"filedate" owner:self];

[cellview.textField setStringValue:flag[@"date"]];

return cellview;

}

return nil;

}

and here what how i'am trying to reload data when creating a new file:

if([identifier isEqualToString:@"newFileModalDone"]){

NSString *newFileName = [self fileNameModalDone];

//creating new file

[TxtFileObj WriteToFile:[newFileName stringByAppendingString:@".txt"] :nil];

//rescaning directory

[TxtFileObj GetFilesList];

FilesArray = [TxtFileObj GetFileArray];

//updating files count

filesCount = [FilesArray count];

[FilesListTable reloadData];

}

i was trying to watch debugger many times and it's seem like exceptions comes from

NSDictionary *flag = FilesArray[row];

so i was thinking about wrong array size after creating new film but i checked it many times and everything was fine.

Please help me to find out what I'am doing wrong, sorry if problem is obvious i am very new to Objective C and COCOA.

Thank you for help!

网友答案:

So after reading some forums i finally found my mistake :)

Not sure if its correct but now its working fine, now code looks like this:

if([identifier isEqualToString:@"newFileModalDone"]){
        NSString *newFileName = [self fileNameModalDone];
        [FilesListTable beginUpdates];
        [TxtFileObj writeToFile:[newFileName stringByAppendingString:@".txt"] :nil];
        [TxtFileObj getFilesList];
        FilesArray = [TxtFileObj filesListArray];
        [FilesListTable insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:[FilesArray count]-1]
                              withAnimation:NSTableViewAnimationSlideDown];

        [FilesListTable endUpdates];
        [FilesListTable reloadData];
    }

what i basically did is added 3 methods:

[FilesListTable beginUpdates];

[FilesListTable insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:[FilesArray count]-1]
                                  withAnimation:NSTableViewAnimationSlideDown];

[FilesListTable endUpdates];
相关阅读:
Top