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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 11-08-2008, 09:33 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 62
Default TableView with grouped style - Not using interface builder

Does anyone have an example showing how to create a grouped table using code not using interface builder. I need to build a view that has different settings that are grouped into their own area. Just like if on the iPhone simulator you goto settings > safari.

Does anyone know how to do this?

I have been playing around with it but cannot seem to get the results I want, and cannot find substantial documentation explaining how it all works.

Thanks a lot.
keyboardcowboy is offline   Reply With Quote
Old 11-09-2008, 12:38 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 243
Default

- (id)initWithStyleUITableViewStyle)style {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
}
return self;
}
noobAppDeveloper is offline   Reply With Quote
Old 11-09-2008, 01:49 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 62
Default

I understand how to set it so that its in grouped mode. But how do I create groups and add rows to each group and stuff like that?

thanks
keyboardcowboy is offline   Reply With Quote
Old 11-09-2008, 02:46 AM   #4 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 401
Default

You still put everything into one array but you edit the following:

Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	NSString *title = nil;
	switch (section) {
		case 0:
			title = @"aaa";
			break;
		case 1:
			title = @"bbb";
			break;
		case 2:
			title = @"ccc";
			break;
		case 3:
			title = @"ddd";
			break;
		default:
			break;
	}
	return title;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section == 0) return 3;
	if (section == 1) return 3;
	if (section == 2) return 4;
	if (section == 3) return 1;
	else return 0;
}
Basically, the first 3 in the array will go in section 1. Then the next 3 will go in section 2. 4 in section 3 and finally 1 in section 4.
__________________
BUZZER! : iTunes Library Music Quiz (1 or 2 Player)
Stitch is offline   Reply With Quote
Old 11-09-2008, 10:30 AM   #5 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Sorry but that's not good advice - the part about storing all rows for multiple sections into a single array. That may work for a static table but it will be a nightmare if the data can change. One possible approach is to keep an array of arrays or a dictionary of arrays. Again, this is one possibility. Each array would correspond to one section. When you get the indexPath you grab the appropriate array using indexPath.section. From that array you get the correct element using indexPath.row.

Using this approach makes your data source much easier because now the method for the number of sections just calls the 'count' property on the top level array. The method for the number of rows in a section returns the 'count' of the second level array.

Don't hardcode such things. It will just make your app more prone to bugs as the data changes.
RickMaddy is offline   Reply With Quote
Old 11-09-2008, 10:35 AM   #6 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 401
Default

"I stand corrected said the man in the orthopedic shoes"

Good advice. Especially the part about calling count on the different arrays (in the main array).

Will definitely use this next time.
__________________
BUZZER! : iTunes Library Music Quiz (1 or 2 Player)
Stitch is offline   Reply With Quote
Old 11-09-2008, 03:47 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 62
Default

Sorry but I am still a little lost. I am still new to iPhone development. here is what I have... inside my view that i am showing on screen

UITableViewController* cont = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
UITableView* table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
cont.tableView = table;
[self addSubview:cont.view];

Plus I added the 3 methods mentioned above that are supposed to help create the sections. But all i get on screen is a regular table view i think. Basically the screen just has about 10 rows on it. I am not sure what I am doing wrong but if someone could explain or has an example of making a grouped table view could you please help me out.

And thanks for the help so far.
keyboardcowboy is offline   Reply With Quote
Old 11-09-2008, 04:03 PM   #8 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Have you looked at the supplied example code? There are several table based example apps that come with the SDK that show how to create and use table views. Most do not use IB for the table views.
RickMaddy is offline   Reply With Quote
Old 11-09-2008, 04:13 PM   #9 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 62
Default

I have looked at these examples. I am a little lost with them. This is why I was hoping someone on here could actually explain how they work. The sample code is good, but some explanations would help a lot.
keyboardcowboy is offline   Reply With Quote
Old 11-09-2008, 04:17 PM   #10 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Have you read the "Table View Programming Guide for iPhone OS". This does a good job of putting it all together. Combined with the example code, this is how most of us learned.
RickMaddy is offline   Reply With Quote
Old 11-12-2008, 03:06 PM   #11 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

I have two arrays of data which are represented in the table.
The table has 3 sections:
A top section and the two arrays.

It all works fine, except a few important methods:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
The problem with these methods is, that I have to know which array (which section) is affected.
How can I handle this?
Why isn't there just one more argument with the section?

I hope you can help me...
Forsworn is offline   Reply With Quote
Old 11-12-2008, 03:38 PM   #12 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

The one parameter has everything you need. Have you looked at the docs for NSIndexPath (UITableView extensions)? It has two properties: row and section.
RickMaddy is offline   Reply With Quote
Old 11-12-2008, 04:14 PM   #13 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

Ohh... I didn't check the tableview category of it... Thank you so much!!
Forsworn 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: 864
15 members and 849 guests
aziz, chiataytuday, fiftysixty, harsha_0409, Ishika Verma, klbaiju, leahov, Morgan72, provalminsk, Shubhank, snackbox, Snail, suksmo, Vineesh, viperfan91
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,297
Threads: 89,032
Posts: 379,810
Top Poster: BrianSlick (7,086)
Welcome to our newest member, Snail
Powered by vBadvanced CMPS v3.1.0

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