TableView

Discussion in 'iOS Programming' started by beit, Aug 18, 2008.

  1. beit macrumors newbie

    Joined:
    May 27, 2008
    #1
    HI guy, I have another time a big problem that I don't understand.

    I'm working with tableView's and I'm trying to create a simple example.

    Code:
    //
    //  EBDisplayList.m
    //  TableView
    //
    //  Created by Beit on 11.08.08.
    //  Copyright 2008 __MyCompanyName__. All rights reserved.
    //
    
    #import "EBDisplayList.h"
    
    
    @implementation EBDisplayList
    @synthesize dictionary;
    
    
    static EBDisplayList *sharedEBDisplayListInstance = nil;
    
    +(EBDisplayList*) sharedEBDisplayList {
    	@synchronized(self) {
    		if (sharedEBDisplayListInstance == nil) {
    			[[self alloc] init];
    		}
    	}
    	return sharedEBDisplayListInstance;
    }
    
    -(id) init {
    	if (self = [super init]) {
    
    	}
    		
    	TestaRossa = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Ferrari",@"TestaRossa",nil] forKeys:[NSArray arrayWithObjects:@"Marca",@"Modello",nil]];
    	F430 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Ferrari",@"F430",nil] forKeys:[NSArray arrayWithObjects:@"Marca",@"Modello",nil]];
    	Maranello = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Ferrari",@"Maranello",nil] forKeys:[NSArray arrayWithObjects:@"Marca",@"Modello",nil]];
    	TT = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Audi",@"TT",nil] forKeys:[NSArray arrayWithObjects:@"Marca",@"Modello",nil]];
    	S3 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Audi",@"S3",nil] forKeys:[NSArray arrayWithObjects:@"Marca",@"Modello",nil]];
    
    	Ferrari = [NSArray arrayWithObjects:TestaRossa, F430, Maranello,nil];
    	Audi = [NSArray arrayWithObjects:TT, S3,nil];
    
    	dictionary = [NSArray arrayWithObjects:Ferrari,Audi,nil];
    
    	return self;	
    }
    
    -(NSDictionary *) dictionaryValueForIndexPath:(NSIndexPath *)indexPath {
    	return ((NSDictionary *)[[dictionary objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]);
    }
    
    -(NSDictionary *) dictionaryValueForSection:(NSInteger)section andRow:(NSInteger)row {
    	return ((NSDictionary *)[[dictionary objectAtIndex:section] objectAtIndex:row]);
    }
    
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
    	//Number of sections is the number of model dictionaries
    	return [dictionary count];
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    	//Number of rows is the number of names in the model dictionary fot the specified section
    	return  [[dictionary  objectAtIndex:section] count];
    }
    
    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Ide"];
    	if (cell == nil) {
    		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 0, 0) reuseIdentifier:@"Ide"] autorelease];
    	}
    
    
    	
    // I TRIED THOSE TWO SOLUTION BUT DOESN'T WORK
    
    	cell.text = [[[[EBDisplayList sharedEBDisplayList] dictionaryValueForIndexPath:indexPath] objectForKey:@"Modello"] description];
    
    	cell.text = [[self dictionaryValueForIndexPath:indexPath] objectForKey:@"Modello"] description];
    
    	return cell;
    }
    
    -(void) dealloc {
    	[dictionary release];
    	//[keys release];
    	//[objects release];
    	[super dealloc];
    }
    @end
    

    My problem is that when I acces at dictionary(is the array containing my cars) the program crash. I can't understand why I can acces from everywhere in my program but when I try to acces from "(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath" it doesn't work.
    I think it can be a storage problem, hte visibilitie of that function isn't the same as the one from my class. I'm destroing my brain on my desk on those day.


    pleaseee helppp meeee :-DDD

    rgds
     
  2. tacoman667 macrumors regular

    Joined:
    Mar 27, 2008
    #2
    Is dictionary a NSArray type? if it is, then you will want to do objectAtIndex:indexPath.section and then use indexPath.row when getting the dictionary item from the nested array.
     
  3. beit thread starter macrumors newbie

    Joined:
    May 27, 2008
    #3
    exactly, is an NSArray and I take data as you said. But the problem is that i can't acces to this array from cellForRowAtIndexPath:. This is the function that use tableView to refrefh table data. I can't understand why if I call objectAtIndex: after creating my array, everything works, but when I'm on the function that tableView utilise for refresh tableView data, the acces to the array don't work.
     
  4. tacoman667 macrumors regular

    Joined:
    Mar 27, 2008
    #4
    Are you making sure to cast your array items properly as you pull them out? I don't see you using indexPath.row or .section as you absolutely need them. This was an addition in the iPhone SDK NSIndexPath object from traditional Mac NSIndexPath.
     
  5. beit thread starter macrumors newbie

    Joined:
    May 27, 2008
    #5
    Yea I use it in the third function,
    Code:
    -(NSDictionary *) dictionaryValueForIndexPath:(NSIndexPath *)indexPath {
    	return ((NSDictionary *)[[dictionary objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]);
    }
    
    and i do the cast when returning the element. Then in my program I use this funtion to get an element. This is right? :-D
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    You "dictionary" will be autoreleased when you create it and will not stick around. You need to either retain it or use alloc/init to create it.

    Read the Cocoa Memory Management document.
     
  7. beit thread starter macrumors newbie

    Joined:
    May 27, 2008
    #7
    you are a Legend :p

    I forgot a cast in a function :-D

    many thx
     

Share This Page