Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-17-2009, 02:36 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 5
LokiHobbs is on a distinguished road
Default Problem with my tables crashing

Like so many people commenting on here, I am new to the world of Iphone development. I am hoping someone can point out to me why my code keeps crashing. Neither the debugger nor the console is providing me with useful feedback.

I am trying to create a simple dictionary using a tableview. I need the Terms to have their first letter as a key and the term to bind to the tableview.

The datasource is a plist that is formatted as such but shrunk for readability here:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>Term</key>
		<string>TERM1</string>
		<key>Definition</key>
		<string>DEFINITION1</string>
	</dict>
	<dict>
		<key>Term</key>
		<string>TERM2</string>
		<key>Definition</key>
		<string>DEFINITION2</string>
	</dict>
</array>
</plist>
I have a model class as follows:
TermsAndDefinitions.h
Code:
#import <Foundation/Foundation.h>

@interface TermsAndDefinitions :  NSObject  
{
	NSString *Term;
	NSString *Definition;
}

- (id)initWithDictionary:(NSDictionary *)aDictionary;

@property (nonatomic, retain) NSString *Term;
@property (nonatomic, retain) NSString *Definition;

@end
TermsAndDefinitions.m
Code:
#import "TermsAndDefinitions.h"

@implementation TermsAndDefinitions 

@synthesize Term;
@synthesize Definition;

- (id)initWithDictionary:(NSDictionary *)aDictionary {
	if ([self init]) {
		self.Term = [aDictionary valueForKey:@"Term"];
		self.Definition = [aDictionary valueForKey:@"Definition"];	
	}
	return self;
}

- (void)dealloc {
	[Term release];
	[Definition release];
	[super dealloc];
}
@end
Definitions.h
Code:
#import <UIKit/UIKit.h>
#import "TermsAndDefinitions.h"

@interface Definitions : UIViewController 
	<UITableViewDataSource, UITableViewDelegate>
 
{
	NSMutableDictionary *definitionsDictionary;	
	NSMutableDictionary *nameIndexesDictionary;
	TermsAndDefinitions *eachTermAndDefinition;
	NSArray *termsNameIndexArray;		
}

@property (nonatomic,retain) NSMutableDictionary *nameIndexesDictionary;
@property (nonatomic,retain) NSMutableDictionary *definitionsDictionary;
@property (nonatomic, retain) TermsAndDefinitions *eachTermAndDefinition;
@property (nonatomic,retain) NSArray *termsNameIndexArray;

@end
Definitions.m
Code:
#import "Definitions.h"
#import "LP_1_0AppDelegate.h"


@interface Definitions(mymethods)

- (void)presortTermNamesForInitialLetter:(NSString *)aKey;
- (void)presortDefinitionLetterIndexes;

@end



@implementation Definitions

@synthesize definitionsDictionary;
@synthesize nameIndexesDictionary;
@synthesize eachTermAndDefinition;
@synthesize termsNameIndexArray;

- (void)viewDidLoad {	
	
	NSDictionary *eachTerm;	
	
	// create dictionaries that contain the arrays of element data indexed by Term
	self.definitionsDictionary = [NSMutableDictionary dictionary];		
	
	// unique first characters (for the Name index table)
	self.nameIndexesDictionary = [NSMutableDictionary dictionary];	
	
	NSString *thePath = [[NSBundle mainBundle]  pathForResource:@"Definitions" ofType:@"plist"];
	NSArray *rawTermsAndDefinitionsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
	
	// iterate over the values in the raw elements dictionary
	for (eachTerm in rawTermsAndDefinitionsArray)
	{		
		// create an Term and Definition instance for each
		TermsAndDefinitions *aTermAndDefinition = [[TermsAndDefinitions alloc] initWithDictionary:eachTerm];
		
		// store the item in the terms and definitions dictionary with the Term as the key
		[definitionsDictionary setObject:aTermAndDefinition forKey:aTermAndDefinition.Term];		
		
		// get the term's initial letter
		NSString *firstLetter = [aTermAndDefinition.Term substringToIndex:1];
		NSMutableArray *existingArray;
		
		// if an array already exists in the term index dictionary
		// create an array and add it to the term index dictionary with the letter as the key
		if (existingArray = [nameIndexesDictionary valueForKey:firstLetter]) 
		{
			[existingArray addObject:aTermAndDefinition];
		} else {
			NSMutableArray *tempArray = [NSMutableArray array];
			[nameIndexesDictionary setObject:tempArray forKey:firstLetter];
			[tempArray addObject:aTermAndDefinition];
		}				
		
		// release the Term and Definition, it is held by the various collections
		[aTermAndDefinition release];		
	}
	
	// release the raw terms data
	[rawTermsAndDefinitionsArray release];		
	
	[self presortDefinitionLetterIndexes];
 }


// return an array of terms for an initial letter (ie A, B, C, ...)
- (NSArray *)termsWithInitialLetter:(NSString*)aKey {
	return [nameIndexesDictionary objectForKey:aKey];
}

// presort the name index arrays so the terms are in the correct order
- (void)presortDefinitionLetterIndexes {
	self.termsNameIndexArray = [[nameIndexesDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	for (NSString *eachNameIndex in termsNameIndexArray) {
		[self presortTermNamesForInitialLetter:eachNameIndex];
	}
}

- (void)presortTermNamesForInitialLetter:(NSString *)aKey {
	NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Term"
																   ascending:YES
																	selector:@selector(localizedCaseInsensitiveCompare:)] ;
	
	NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
	[[nameIndexesDictionary objectForKey:aKey] sortUsingDescriptors:descriptors];
	[nameDescriptor release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	self.definitionsDictionary = nil;
	self.nameIndexesDictionary = nil;
	self.eachTermAndDefinition = nil;
	[super viewDidUnload];	
}

- (void)dealloc {
	[definitionsDictionary release];
	[nameIndexesDictionary release];
	[eachTermAndDefinition release];
        [super dealloc];
}

#pragma mark - 
#pragma mark Table View Data Source Methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
		return [termsNameIndexArray count];
} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
	{ return termsNameIndexArray; }

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
	return index;
}

- (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section {
	// the section represents the initial letter of the term
	// return that letter
	NSString *initialLetter = [termsNameIndexArray objectAtIndex:section];
	
	// get the array of terms that begin with that letter
	NSArray *termsWithInitialLetter = [termsWithInitialLetter:initialLetter];
	
	// return the count
	return [termsWithInitialLetter count];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	// this table has multiple sections. One for each unique character that a term begins with
	// [A,B,C,D,E,F,G,H,I,K,L,M,N,O,P,R,S,T,U,V,X,Y,Z]
	// return the letter that represents the requested section
	
	return [termsNameIndexArray objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSUInteger section = [indexPath section];
	NSUInteger row = [indexPath row];
	
	NSString *key = [termsNameIndexArray objectAtIndex:section];
	NSArray *termsSection = [nameIndexesDictionary objectForKey:key];
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	
	static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
		
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier];
	if (cell == nil) { 
		cell = [[[UITableViewCell alloc]
				initWithStyle:UITableViewCellStyleDefault 
				reuseIdentifier:SectionsTableIdentifier] autorelease];
	}
	cell.textLabel.text = [termsSection objectAtIndex:row]; 
	return cell;
}

@end
I am usually all for working through an issue but it has been a few days now and I cannot see it. I am hoping it is something silly that I just do not get in Ojective C.

Thanks

Dave

Last edited by LokiHobbs; 12-17-2009 at 04:27 PM.
LokiHobbs is offline   Reply With Quote
Old 12-17-2009, 05:18 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 1
doorak is on a distinguished road
Default plist

What is it you are trying to do? Are you creating an array from the PLIST and then creating another array that is the first letter of the terms then using it as a dictionary for the Terms array?
doorak is offline   Reply With Quote
Old 12-17-2009, 09:46 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 5
LokiHobbs is on a distinguished road
Default GRRRRRR

Exactly. But it is just not working. Any suggestions?
LokiHobbs is offline   Reply With Quote
Old 12-17-2009, 10:45 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
eddietr is on a distinguished road
Default

What error do you see in the console? Do you know where the app is crashing?
eddietr is offline   Reply With Quote
Old 12-17-2009, 11:03 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 5
LokiHobbs is on a distinguished road
Default I am not seeing any error in the console

Quote:
Originally Posted by eddietr View Post
What error do you see in the console? Do you know where the app is crashing?
I am not seeing any error in the console. That is what is frustrating. It shows the start up when I build the app. But nothing beyond that. I had been using the console to give me hints which has been working to some degree up until now.
LokiHobbs is offline   Reply With Quote
Old 12-17-2009, 11:06 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
eddietr is on a distinguished road
Default

Quote:
Originally Posted by LokiHobbs View Post
I am not seeing any error in the console. That is what is frustrating. It shows the start up when I build the app. But nothing beyond that. I had been using the console to give me hints which has been working to some degree up until now.
And you're running it in the debugger and not seeing a stack when it crashes?

Maybe you can post the project and someone can take a quick look.
eddietr is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutabledictionary, tableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
13 members and 316 guests
akacaj, alexP, ClerurcifeDer, Duncan C, givensur, GraffitiCircus, guusleijsten, JmayLive, NetGuru, Paul Slocum, Punkjumper, Sloshmonster, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0