app crahes at reloadData

Discussion in 'iOS Development' started by hobofan, Aug 17, 2008.

  1. hobofan

    hobofan New Member

    Joined:
    Jun 1, 2008
    Messages:
    68
    Likes Received:
    0
    I tried to reload the data of an UITable with a different dataset, but always i try to do this, my app crashes.

    here's my code: (I know, it's a bit unclean, and some functions aren't in use)

    Code:
    @end
    
    @implementation MobileCoding
    
    /* This is what happens when the application is launched */
    
    - (void)applicationDidFinishLaunching:(id)unused
    {		
    	action = @"MainMenu";
    
    	rect = [ UIHardware fullScreenApplicationContentRect ];	
    	rect.origin.x = 0.0f;
    	rect.origin.y = 0.0f;	
    		
    	window = [ [ UIWindow alloc ] initWithContentRect: rect ]; // This made it the size of our full screen rectangle
    	mainView = [ [ UIView alloc ] initWithFrame: rect ];
    	
    	navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(40, 200, 480, 40)];
    	[navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle:@"Main Menu" ] ];
    	//[navBar showButtonsWithLeftTitle:@"Hide KB" rightTitle:@"Menu" leftBack:NO];
    	[navBar setBarStyle:5]; // This sets the color and look of the navigation bar.
    	[navBar setRotationBy: 90 ];
    	[navBar setDelegate:self];
    
    	menutable = [[UITable alloc] initWithFrame:CGRectMake(-110, 90, 480, 260)];
    	[menutable setRotationBy: 90 ];
    	[menutable setDelegate:self];
    
    	[mainView addSubview:menutable];
    								
    	UITableColumn *col = [[UITableColumn alloc] initWithTitle:@"menu" identifier:@"menu" width:480.0f];
        
        	[menutable addTableColumn:col];
        	[menutable setDataSource:self];
        	[menutable setDelegate:self];
    	[menutable setSeparatorStyle: 1];
        	[menutable reloadData];
    
    	/*txtView = [[EditTextView alloc] initWithFrame:CGRectMake(-130, 70, 480, 300)];
    	[txtView setRotationBy: 90 ];
    	[txtView setDelegate:self];*/
    	
    	
    	[mainView addSubview:navBar];
    	[mainView addSubview:menutable];
    	//[mainView addSubview:txtView];
    
    	[ window makeKeyAndVisible ];		
    		
    	[ window setContentView: mainView ];
    	[ self setStatusBarMode: 0 orientation: 90 duration: 0 ];		
    }
    
    
    - (void)textViewDidBeginEditing:(UITextView *)textView
    {
    
    }
    
    - (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button 
    {
    	if(action == @"Editor")
    	{
    		if(button == 0) 
    		{
    			action = @"Menu";
    
    			[txtView resignFirstResponder];
    
    		 	[navBar showButtonsWithLeftTitle:@"Editor" rightTitle:nil leftBack:YES];				
    
    			/*[txtView dealloc];*/	
    
    			menutable = [[UITable alloc] initWithFrame:CGRectMake(-110, 90, 480, 260)];
    			[menutable setRotationBy: 90 ];
    			[menutable setDelegate:self];
    
    			[mainView addSubview:menutable];
    								
    			UITableColumn *col = [[UITableColumn alloc] initWithTitle:@"menu" identifier:@"menu" width:480.0f];
        
        			[menutable addTableColumn:col];
        			[menutable setDataSource:self];
        			[menutable setDelegate:self];
    			[menutable setSeparatorStyle: 2];
        			[menutable reloadData];			
    		}
    		else if(button == 1)
    		{
    			[txtView resignFirstResponder];
    		}	
    	}
    	else if(action == @"Menu")
    	{
    		if(button == 1)
    		{
    			/*[menutable dismiss];*/
    
    			txtView = [[EditTextView alloc] initWithFrame:CGRectMake(-130, 70, 480, 300)];
    
    			[navBar showButtonsWithLeftTitle:@"Hide KB" rightTitle:@"Menu" leftBack:NO];
    
    			action = @"Editor";
    		}
    	}
    }
    
    - (int)numberOfRowsInTable:(UITable*)table
    {	 
    	int i;
    
    	if(action == @"MainMenu")
    	{
    		i = 3;
    	}
    	else if(action == @"Projects")
    	{
    		i = [directoryArray count];
    	}
    	else if(action == @"Menu")
    	{
    		i = 3;
    	}	
    
    	return i;
    }
    
    - (UITableCell *)table:(UITable *)table cellForRow:(int)row column:(int)column
    {	 		 
    	UIImageAndTextTableCell* cell = [ [ [ UIImageAndTextTableCell alloc ] init] autorelease ];
    	if(action == @"Menu")
    	{
    		if(row == 0)
    		{
    			[cell setTitle: @"Save Changes"];
    			[cell setShowDisclosure: YES];
    			[cell setDisclosureStyle: 0];
    		}
    		if(row == 1)
    		{
    			[cell setTitle: @"Compile!"];
    			[cell setShowDisclosure: NO];
    		}	
    		if(row == 2)
    		{
    			[cell setTitle: @"Project"];
    			[cell setShowDisclosure: YES];
    			[cell setDisclosureStyle: 0];
    		}
    	}
    	else if(action == @"Projects")
    	{
    		[cell setTitle: [NSString stringWithFormat: @"%@", [directoryArray objectAtIndex: row]]];
    		[cell setShowDisclosure: YES];
    		[cell setDisclosureStyle: 0];		
    	}
    	else if(action == @"MainMenu")
    	{
    		if(row == 0)
    		{
    			[cell setTitle: @"Projects"];
    			[cell setShowDisclosure: YES];
    			[cell setDisclosureStyle: 0];
    		}
    		if(row == 1)
    		{
    			[cell setTitle: @"Preferences"];
    			[cell setShowDisclosure: YES];
    			[cell setDisclosureStyle: 0];
    		}
    		if(row == 2)
    		{
    			[cell setTitle: @"About"];
    			[cell setShowDisclosure: NO];
    		}				
    	}	
    	
    	return [cell autorelease];
    }
    
    - (void)tableRowSelected:(NSNotification*)notification
    {
    	if(action == @"MainMenu")
    	{
    		if(menutable.selectedRow == 0)
    		{
    			action = @"Projects";
    			
    			directoryManager = [NSFileManager defaultManager];
    			directoryArray = [[NSArray alloc]initWithArray:[directoryManager directoryContentsAtPath:@"/private/var/stash/Applications.hvqJ7z/MobileCoding.app/Projects/"]];
    
    			[menutable reloadData];							
    		}
    		else if(menutable.selectedRow == 1)
    		{
    			
    		}
    		else if(menutable.selectedRow == 2)
    		{
    			NSArray *buttons = [NSArray arrayWithObjects:@"OK", nil, nil];
    			UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithTitle:@"About" buttons:buttons defaultButtonIndex:1 delegate:self context:self];
    			[alertSheet setBodyText:@"MobilCoding by Hobofan"];
    			[alertSheet popupAlertAnimated:YES];
    		}
    	}
    }
    
    @end
  2. hobofan

    hobofan New Member

    Joined:
    Jun 1, 2008
    Messages:
    68
    Likes Received:
    0
    I fixed the problem that it crashes, but for some reason, it doesn't show any of the files:

    heres the new code:

    NSFileManager *fileManager = [ NSFileManager defaultManager ];
    NSDirectoryEnumerator *dirEnum;
    NSString *file;
    NSString *path;
    NSString *extension;

    path = @"/private/var/stash/Applications.hvqJ7z/MobileCoding.app/Projects/first/";
    extension = @"";

    if ([ fileManager fileExistsAtPath: path ] == NO) {
    return;
    }

    [ directoryArray removeAllObjects ];

    dirEnum = [ [ NSFileManager defaultManager ] enumeratorAtPath: path ];
    while ((file = [ dirEnum nextObject ])) {
    if ([ file hasSuffix: extension ] == YES) {
    [ directoryArray addObject: file ];
    }
    }

    edit: problem solved, I've forgoten to initialize the directoryArray

    Please Register or Log in to view images

  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Whoa, is the variable of path a hard link to your Application?

    If it is, change it immediately, else your app will fail on everyone's device other than yours. You can programatically get the path of your app by using NSBundle.

    Code:
    NSString *bundle = [NSBundle mainBundle];
    NSString *path - [[NSString alloc] initWithFormat:@"%@/MobileCoding.app/Projects/first/",bundle];
    Although, I can't remember if you need that initial / after the placeholder. Try it both ways and use the one that works.

    Please Register or Log in to view images

  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    why is it we see this SAME DAMN CODE everywhere? gahr it makes me mad to see so much copy and paste out of nervegas book. its almost like nobody knows how to make these things themselves
  5. hobofan

    hobofan New Member

    Joined:
    Jun 1, 2008
    Messages:
    68
    Likes Received:
    0
    thx for the tip skylar, but always if i use:

    Code:
    NSString *bundle = [NSBundle mainBundle];
    I get a error like this:

    Code:
    warning: initialization from distinct Objective-C type
    i searched in google and found, that the returned "mainBundle" isn't a NSString.

    Is there any way to fix that error???
  6. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    say you have a subfolder in your app called like Files. then there was a data.plist in that. use this

    Code:
    [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist" inDirectory:@"Files"
    also, for what you're trying to do above i believe its;

    Code:
    NSString* bundle = [[NSBundle mainBundle] bundlePath]

Share This Page