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 06-06-2010, 08:05 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default Display two data cells in UITableView Row

Hi all,

I am trying to work out how I can display two data cells in a UITableView Row.

All my data is stored in an SQLite table.

At the moment I run a query to extract a list of countries into an array "arrayOfCharacters" and also create another array "objectsForCharacters".

The arrayOfCharacters is to create an A - Z index for use in table sections and the objectsForCharacters hold the names of countries.

In cellForRowAtIndexPath I display the name of the country but I would like to display the capital city at the same time. The fields in the table that hold the data are called "countryName and CountryCapital. How can I display the countryCapital.

I have been playing around with this for days without any success, can any help me out.

The code:
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]]; 
	
	return cell;
	
}
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 06-06-2010, 09:43 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Long Beach, CA
Posts: 612
bytor99999 is on a distinguished road
Send a message via AIM to bytor99999 Send a message via Yahoo to bytor99999
Default

Well, you are using the stock TableCell in your Table, which just has one textfield. So you could just create a string that has both values and set that one textfield to that value.

You have, which adds just one string value.

Code:
// Set up the cell
	cell.textLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
Using NSString's stringWithFormat method, you can concatenate two values into one string like

Code:
// Set up the cell
	cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row], <<<<put in call to get your other string;
The other option is always to create your own TableCellView, add two UITextFields and set one to one value and the other to the other.

If all you want is just a string with those values, I would go the first route. But if you want it to look different, or say one text value above the other, or some other stuff, then a custom tablecell view is the way to go.

Mark
__________________
Perfect World Programming LLC
http://www.perfectworldprogramming.com

Please check out my apps.

TubeOrganizer
http://www.spritzlerapps.com/tube-organizer.html

Paper Clips
http://spritzlerapps.weebly.com/paper-clips.html
bytor99999 is offline   Reply With Quote
Old 06-06-2010, 10:03 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi bytor99999,

Thanks for your reply.

I think my problem is I am not sue how to make the call to get the other string. the code below is how I am running my query to get the data. How would you go about getting the name of the "countryCapital" so it could be displayed.

Thanks for your time with this.
Code:
arrayOfCharacters = [[NSMutableArray alloc]init];

	objectsForCharacters = [[NSMutableDictionary alloc]init];
	
	sqlite3 *db = [TravelQAppDelegate getNewDBConnection];
	for(char c='A';c<='Z';c++)
	{
		NSMutableString *query;
		
		query = [NSMutableString stringWithFormat:@"select countryName, countryCapital, 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",sqlite3_errmsg(db));
			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];
		}
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 06-06-2010, 12:27 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

You would just call sqlite3_column_text(statement, 1) to get your countryCapital. This will get you the value from your second column returned by your select statement.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 06-06-2010, 12:47 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi dljeffery


I have:
Code:
NSMutableArray *arrayOfNames = [[NSMutableArray alloc]init];
while(sqlite3_step(statement)==SQLITE_ROW)
	[arrayOfNames addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement, 0)]];
	[arrayOfNames addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement, 1)]];
	if([arrayOfNames count] >0) {
		[arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]];
		[objectsForCharacters setObject:arrayOfNames forKey:[NSString stringWithFormat:@"%c",c]];
}
Using bytor99999 code
Code:
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row], <<<<put in call to get your other string;
How would I call the content of "countryCapital"

I am sorry to be a pain in the "lets just say neck" and I do appreciate your time.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 06-06-2010, 12:57 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

I would do this instead:

Code:
NSMutableArray *arrayOfNames = [[NSMutableArray alloc]init];
while(sqlite3_step(statement)==SQLITE_ROW)
	[arrayOfNames addObject:[NSString stringWithFormat:@"%s (%s)", 
(char*)sqlite3_column_text(statement, 0), 
(char*)sqlite3_column_text(statement, 1)]];
Or however you want it formatted... this example would be formatting it as something like "Japan (Tokyo)". Seems like adding two objects to your array for each entry is just more confusing. Another thing I'd consider is creating some sort of custom class or struct to track the contents of each record, and add the record into your array.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com

Last edited by dljeffery; 06-06-2010 at 12:58 PM. Reason: originally swapped country and capital... oops
dljeffery is offline   Reply With Quote
Old 06-06-2010, 01:10 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi dljeffery,

Many thanks again for your input. I think I need to go and give this some thought.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-10-2010, 08:19 AM   #8 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi all,

I have an SQLite query that extracts data from a table and writes it to an array.
Code:
NSMutableArray *arrayOfNames = [[NSMutableArray alloc]init];
			while(sqlite3_step(statement)==SQLITE_ROW)
				[arrayOfNames addObject:[NSString stringWithFormat:@"%s" @"%s", 
										 (char*)sqlite3_column_text(statement, 0), 
										 (char*)sqlite3_column_text(statement, 1)]];
			
			if([arrayOfNames count] >0)
			{
				[arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]];
				
				[objectsForCharacters setObject:arrayOfNames forKey:[NSString stringWithFormat:@"%c",c]];
			}
			[arrayOfNames release];
I then use the array to populate a grouped tableview.

The array contain 2 fields from the data table "countryName" and "countryFlag". If I echo out the content of the array I get the following:
Code:
A =     (
        AbkhaziaAbkhazia1,
        "Afghanistan Afghanistan",
        "Albania Albania",
        "Algeria Algeria",
        "American SamoaAmericanSamoa",
        "Andorra Andorra",
        "Angola Angola",
        AnguillaAnguilla,
        "Antigua and Barbuda Antigua",
        "Argentina Argentina",
        "Armenia Armenia",
        ArubaAruba,
        "Australia Australia",
        "Austria Austria",
        AzerbaijanAzerbaijan,
        AzoresAzores
    );
and the same for all the countries starting with "B", 'C" and so on.

The code that produces the tableview is:
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]]; 
	
	//UIImage *image = [UIImage imageNamed: countryFlag ];
	//cell.imageView.image = image;
return cell;	
}
What I am trying to do is to use the second index of the array to display the countryFlag. I have been trying to do this for days without success. And anyone provide some help.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-10-2010, 08:35 AM   #9 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

Quote:
Originally Posted by bytor99999 View Post
Well, you are using the stock TableCell in your Table, which just has one textfield. So you could just create a string that has both values and set that one textfield to that value.

You have, which adds just one string value.

Code:
// Set up the cell
	cell.textLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
Using NSString's stringWithFormat method, you can concatenate two values into one string like

Code:
// Set up the cell
	cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row], <<<<put in call to get your other string;
The other option is always to create your own TableCellView, add two UITextFields and set one to one value and the other to the other.

If all you want is just a string with those values, I would go the first route. But if you want it to look different, or say one text value above the other, or some other stuff, then a custom tablecell view is the way to go.

Mark
I think the correct and simplest way of displaying two texts on a table view cell, is to use the UITableViewCellStyleSubtitle style and then set the textLabel and detailTextLabel.
Ice_2k is offline   Reply With Quote
Old 08-10-2010, 08:51 AM   #10 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi Ice_2k,

The second text from the array is the name of the country and I want to use it to display the country flag. I have all the flag images in the App which are named after the country, example: if the country is "Romania" the the content of the countryFlag would be "romania".

I then want to display the flag "romania" in the table row to the left of the country name. The code that I am playing around with is below, somewhat of a mess at the moment:
Code:
// 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]]; 		

	UIImage *image = [UIImage imageNamed:[[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]];
	cell.imageView.image = countryFlag;
What I am trying to do is somehow use the following code to display the image, the problem is I am completely lost.
Code:
UIImage *countryFlag = [[UIImage alloc]  initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[countryFlag objectAtIndex:13] ofType:@"gif"]];
Can you see how this can be done.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-10-2010, 09:00 AM   #11 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

I'm not sure I understand, what exactly is not working?
Ice_2k is offline   Reply With Quote
Old 08-10-2010, 09:17 AM   #12 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi Ice_2k,

This is the code that produces the rows in the grouped UITableView
Code:
cell.textLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
The output for group A in the simulator displays:

Abkhazia Abkhazia
Afghanistan Afghanistan
Albania Albania
Algeria Algeria
American Samoa American Samoa

What it should display is:

Abkhazia
Afghanistan
Albania
Algeria
American Samoa

and so on.

What I am trying to do is use the content of the second name as a variable for each row to display the country flag as an image. As I was saying I have all the country flag images in the App and they are all .gif images.

I have tried all ways to do this without success which is partly down to my lack of experience with Objective-C.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-10-2010, 09:21 AM   #13 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

Code:
UIImage *countryFlag = [[UIImage alloc]  initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[countryFlag objectAtIndex:13] ofType:@"gif"]];
Does "[countryFlag objectAtIndex:13]" return the correct value? Is countryFlag nil after this call? If you attach your code, I'll take a look at it.
Ice_2k is offline   Reply With Quote
Old 08-10-2010, 09:41 AM   #14 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi Ice_2k, many thanks for your time.

I have attach the .m file.
Attached Files
File Type: zip CountryTableViewController.doc.zip (9.4 KB, 21 views)
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-10-2010, 09:48 AM   #15 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

Please add the entire project (or a compilable section, containing the problem), I intend to build it and debug it, its much faster than just looking at the code.
Ice_2k is offline   Reply With Quote
Old 08-10-2010, 09:58 AM   #16 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi Ice_2k,

Unfortunately I cannot attach the project due to reasons I have no control over.

I know it would be easier to debug but I can't at this time.

Many thanks for any help you can give me.
__________________
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: 334
12 members and 322 guests
Absentia, Domele, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, PixelInteractive, raihan.zbr, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,117
Posts: 402,891
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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