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 08-09-2010, 09:59 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default image in table cell row

Hi all,

I have a table which load a list of countries from an SQLite table. The table holds the country name and also a reference to the country flag. I would like to display the country flag for each country but I am not too sure how I should approach this.

The code I am using is:

To initialize the table data I am using:
Code:
-(void)initializeTableData
{
	
	arrayOfCharacters = [[NSMutableArray alloc]init];
	
	objectsForCharacters = [[NSMutableDictionary alloc]init];
	
	sqlite3 *db = [iTravelv113AppDelegate getNewDBConnection];
	for(char c='A';c<='Z';c++)
	{
		NSMutableString *query;
		
		query = [NSMutableString stringWithFormat:@"select countryName, countryFlag from countryList where countryName LIKE '%c%%'",c];
		
		const char *sql =  [query UTF8String];
		sqlite3_stmt *statement = nil;
		
		if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK)
			
			NSAssert1(0,@"error preparing statement: '%s'", sqlite3_errmsg(db));
		else
			
		{
			NSMutableArray *arrayOfNames = [[NSMutableArray alloc]init];
			while(sqlite3_step(statement)==SQLITE_ROW)
				[arrayOfNames addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement, 0)]];
			
			if([arrayOfNames count] >0)
			{
				[arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]];
				
				[objectsForCharacters setObject:arrayOfNames forKey:[NSString stringWithFormat:@"%c",c]];
			}
			[arrayOfNames release];
		}
		
		
		
		
		
		sqlite3_finalize(statement);
	}
	
}
To create the table row and cell I am using:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// Set up the cell  
	
	cell.textLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
	[[cell textLabel] setTextColor: [UIColor colorWithRed:139.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0]]; 
	
	NSLog(@"Char:%@",[arrayOfCharacters description]); 
	
	UIImage *image = [UIImage imageNamed:@"Anguilla.gif"];
	cell.imageView.image = image;

	
	
	return cell;
	
}
How can I code the part marked in red to display the image relating to the country.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-09-2010, 11:15 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

You could include all of the images in your app, and put the name of the image file correspondign to each country in the database, e.g "Anguila.jpg", "USA.jpg", etc

Then, your cellForRowAtIndex:

Code:
...
UIImage *image = [UIImage imageNamed: theImageFileName ];
	cell.imageView.image = image;

....
where theImageFileName would be the value selected from the database that is the name of the image file.
smithdale87 is offline   Reply With Quote
Old 08-10-2010, 04:47 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi smithdale87,

Many thanks for your reply.

The issue I have is the data array I have which holds the country names, I don't seem to be able to include the field for the countryFlag. the code below produces a list of countries grouped by the first letter of the country, along with an A-Z list for the side jump to list.

How can I include the countryFlag field into the "objectsForCharacters" array.

Any help would be great.

Code:
-(void)initializeTableData
{
	
	arrayOfCharacters = [[NSMutableArray alloc]init];
	
	objectsForCharacters = [[NSMutableDictionary alloc]init];
	
	sqlite3 *db = [iTravelv113AppDelegate getNewDBConnection];
	for(char c='A';c<='Z';c++)
	{
		NSMutableString *query;
		
		query = [NSMutableString stringWithFormat:@"select countryName, countryFlag from countryList where countryName LIKE '%c%%'",c];
		
		const char *sql =  [query UTF8String];
		sqlite3_stmt *statement = nil;
		
		if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK)
			
			NSAssert1(0,@"error preparing statement: '%s'", sqlite3_errmsg(db));
		else
			
		{
			NSMutableArray *arrayOfNames = [[NSMutableArray alloc]init];
			while(sqlite3_step(statement)==SQLITE_ROW)
				[arrayOfNames addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement, 0)]];
			
			if([arrayOfNames count] >0)
			{
				[arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]];
				
				[objectsForCharacters setObject:arrayOfNames forKey:[NSString stringWithFormat:@"%c",c]];
			}
			[arrayOfNames release];
		}
		
		
		
		
		
		sqlite3_finalize(statement);
	}
	
}
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Reply

Bookmarks

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: 328
9 members and 319 guests
HemiMG, ilmman, iram91419, linkmx, nadav@webtview.com, Objective Zero, Paul Slocum, stanny, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

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