Re: MutableArray and TableView Question
Re: MutableArray and TableView Question
- Subject: Re: MutableArray and TableView Question
- From: Chris Share <email@hidden>
- Date: Tue, 19 Oct 2010 21:42:39 -0700 (PDT)
Thanks to all who have responded to my question. Be aware that I'm a complete
newb to Cocoa/Obj-C so it's quite possible that I'm doing something "bad"!
I've managed to fix what was going wrong in my code.
I'm using the following in my program:
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row {
if(tableView == inputTable) {
NSLog(@"Handle input table.");
} else {
NSLog(@"Handle output table.");
}
return[NSStringstringWithFormat:@"%@", [inputArray objectAtIndex:row]];
}
It seems that this method is being called after the inputArray has been emptied
but before the openDlg (see below) is displayed. This means that there's an
error related to what gets returned. I'm not sure why but it could be because
there's some redrawing
going on (or something similar)?
So I moved
[inputArray removeAllObjects];
so that now the code is:
// process the files.
if([openDlg runModalForDirectory:nilfile:nil] == NSOKButton) {
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];
[inputArrayremoveAllObjects]; <- New line here.
// Loop through all the files and process them.
for(i = 0; i < [files count]; i++) { ...
And now it works!
Cheers,
Chris
----- Original Message ----
From: Chris Share <email@hidden>
To: email@hidden
Sent: Tue, 19 October, 2010 5:02:48 PM
Subject: MutableArray and TableView Question
I'm developing an app in which the user is able to navigate to a folder and
select files. A list of the selected files for each folder is displayed in a
table. This process is repeated by the user. Each time the user navigates to a
new folder and selects a new group of files the process starts from the
beginning (i.e. the list of files shown in the table does not increase
cumulatively, it resets each time, only showing the files in the current
folder).
I'm using an NSMutable array as my data source for the table. The problem I'm
having is in clearing the array for each new folder.
Here's the coder that I'm using:
-(IBAction)openButtonClick:(id)sender {
NSLog(@"Open button clicked!");
int i;
// Clear the input array.
[inputArray removeAllObjects];
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanelopenPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
// Disable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:NO];
// Enable the selection of files in the dialog.
[openDlg setAllowsMultipleSelection:YES];
// Display the dialog. If the OK button was pressed,
// process the files.
if([openDlg runModalForDirectory:nilfile:nil] == NSOKButton) {
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];
// Loop through all the files and process them.
for(i = 0; i < [files count]; i++) {
NSString* fileName = [files objectAtIndex:i];
[inputArray addObject:fileName];
}
[inputTablereloadData];
}
}
When I run this code I get the following error:
*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)
I assume that it's the [inputArray removeAllObjects] that's causing the problem
(?) If I remove this line, the code works but the filenames keep getting added
to the array which isn't what I want.
How do I fix this?
Cheers,
Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden