08-09-2010, 09:59 AM
#1 (permalink )
Registered Member
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
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
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
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.
08-10-2010, 04:47 AM
#3 (permalink )
Registered Member
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
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
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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