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

beit

macrumors newbie
Original poster
May 27, 2008
16
0
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
 

tacoman667

macrumors regular
Mar 27, 2008
143
0
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

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.
 

beit

macrumors newbie
Original poster
May 27, 2008
16
0
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.
 

tacoman667

macrumors regular
Mar 27, 2008
143
0
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.

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.
 

beit

macrumors newbie
Original poster
May 27, 2008
16
0
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
 

beit

macrumors newbie
Original poster
May 27, 2008
16
0
you are a Legend :p

I forgot a cast in a function :-D

many thx
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.