1. beit's Avatar
    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 *) tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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

    2008-08-18 06:57 PM
  2. NetMage's Avatar
    The problem with method 1 appears to be that the variable sharedEBDisplayListInstance is never set to any value anywhere.
    In other words, the class method
    +(EBDisplayList*) sharedEBDisplayList
    will always return nil.

    When you test it for nil, you need to assign your new object to it.

    For the second method, I would suggest your instance variable dictionary is mis-named since it is actually an NSArray of NSArray of NSDictionary.

    However, I think your main issue in the second case is that you created the dictionary object using the convenience method:
    dictionary = [NSArray arrayWithObjects:Ferrari,Audi,nil];

    What this means is that your new NSArray object will be autoreleased (deallocated) when the main UI event loop reaches the end of this iteration. The same is true of your nested NSArray's but (alarmingly) not of your NSDictionary objects.

    Since NSArray retains its members, it is correct for the nested NSArray to be autorelease, and it would be wise if the NSDictionary members were autorelease. However, the dictionary object itself needs to exist for the lifetime of the EBDisplayList instance, and so needs to be created as follows:
    dictionary = [[NSArray alloc] initWithObjects:Ferrari,Audi,nil];

    and it must be disposed of in dealloc as you have it now.
    Last edited by NetMage; 2008-08-19 at 12:13 AM.
    Starlight Computer Wizardry
    Pocket-sized Development
    Follow me on twitter: @NetMage
    2008-08-18 11:48 PM
  3. beit's Avatar
    Can I kiss you :-P?

    no really many thx for the explaination...can you give me a hint for where I can read more about autorelease, retain and all these type of proprieties?

    2008-08-19 11:03 AM
  4. NetMage's Avatar
    I can't find where I originally learned this stuff, but this isn't bad:
    Programming Mac OS X with Cocoa for beginners/Some Cocoa essential principles - Wikibooks, collection of open-content textbooks
    Starlight Computer Wizardry
    Pocket-sized Development
    Follow me on twitter: @NetMage
    2008-08-20 12:53 AM