Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bker

macrumors newbie
Original poster
May 22, 2010
6
0
Hi,

I have a tableView and an open dir button. i would like to list all files in a folder in the tableView when i push the open button. Here is what i did, it works well but only when i push the button it does not work. can you please help me. thanks!


PHP:
//
//  FileList.m
//  FileList

#import "FileList.h"


@implementation FileList

- (IBAction)buttonOpen:(id)sender {
	// Open panel
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setTreatsFilePackagesAsDirectories:NO];
	[openPanel setCanChooseFiles:NO];
	[openPanel setAllowsMultipleSelection:NO];
	[openPanel setCanChooseDirectories:YES];
	[openPanel beginSheetForDirectory:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"]
								 file:nil
								types:nil
					   modalForWindow:window
						modalDelegate:self
					   didEndSelector:@selector(didEndOpenSheet:returnCode:contextInfo:)
						  contextInfo:NULL];
}

- (void)didEndOpenSheet:(NSOpenPanel *)openPanel returnCode:(int)returnCode contextInfo:(void *)contextInfo {
	if (returnCode == NSOKButton) {
		[[openPanel filename] fileSystemRepresentation];
		path = [[openPanel filename] stringByStandardizingPath];
		[self fileList]; // When i push the button i would like to list files in tableView
	}
}

- (NSString *)directory {
	NSString *directory = [NSString stringWithFormat:@"%@/", path];
	return directory;
}

- (NSArray *)fileList {
	NSMutableArray *array = [NSMutableArray array];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *fileList = [fileManager directoryContentsAtPath:[self directory]];
	for (int j = 0; j < [fileList count]; j++) {
		NSString *file = [NSString stringWithFormat:@"%@%@", [self directory], [fileList objectAtIndex:j]];
		[array addObject:file];
	}
	return array;
}

// Begin tableView
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
	return [[self fileList] count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
    NSString *identifier = [tableColumn identifier];
    if ([identifier isEqualToString:@"index"]) {
        return [NSString stringWithFormat:@"%d", row + 1];
    }
    if ([identifier isEqualToString:@"value"]) {
		return [[self fileList] objectAtIndex:row];
    }
    return @"";
}

- (void)tableViewSelectionDidChange:(NSTableView *)tableView {
    NSLog(@"test");
}

@end
 
You need to call [table reloadData] to update the table view whenever its data changes.
 
I am working on something very similar, but I need a way to find out when the files in the folder change. Is there some kind of NSNotification I can use to monitor the folder, to then call reloadData?
 
thanks for that kainjow, I managed to setup the fsevent, but i am not sure on how to call this function from with the callback:
Code:
- (void) reloadFlashTableView
{	
	[flashArrayController setContent:[self flashContentArray]];
	[flashTableView reloadData];
	[self displayFlashCount];
}

and the callback:
Code:
void flashCallback(ConstFSEventStreamRef streamRef,
					   void *clientCallBackInfo,
					   size_t numEvents,
					   void *eventPaths,
					   const FSEventStreamEventFlags eventFlags[],
					   const FSEventStreamEventId eventIds[])
{
	[SCPreferencesModule performSelectorOnMainThread:@selector(reloadFlashTableView) withObject:nil waitUntilDone:YES]; .....this causes the error...
}

error:
Code:
28/05/10 11:10:48 AM	Safari[50061]	+[SCPreferencesModule reloadFlashTableView]: unrecognized selector sent to class 0x1162e9f20
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.