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

  1. beit macrumors newbie

    May 27, 2008
    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.

    //  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];
    	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];

    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

  2. tacoman667 macrumors regular

    Mar 27, 2008
    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

    May 27, 2008
    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

    Mar 27, 2008
    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

    May 27, 2008
    Yea I use it in the third function,
    -(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


    Jul 24, 2002
    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

    May 27, 2008
    you are a Legend :p

    I forgot a cast in a function :-D

    many thx

Share This Page