Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mekopolis

macrumors regular
Original poster
Feb 10, 2008
152
0
in this example, from theElements code provided by Apple, they Sort an Array alphabetically by Name, they also provide headers for each section (i.e: a,b,c,d etc)


Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	// this table has multiple sections. One for each unique character that an element 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 count of that array
	return [[[PeriodicElements sharedPeriodicElements] elementNameIndexArray] count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
	// returns the array of section titles. There is one entry for each unique character that an element 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 [[PeriodicElements sharedPeriodicElements] elementNameIndexArray];
}

- (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 element
	// return that letter
	NSString *initialLetter = [[[PeriodicElements sharedPeriodicElements] elementNameIndexArray] objectAtIndex:section];
	
	// get the array of elements that begin with that letter
	NSArray *elementsWithInitialLetter = [[PeriodicElements sharedPeriodicElements] elementsWithInitialLetter:initialLetter];
	
	// return the count
	return [elementsWithInitialLetter count];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	// this table has multiple sections. One for each unique character that an element 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
	// this is actually a delegate method, but we forward the request to the datasource in the view controller
	
	return [[[PeriodicElements sharedPeriodicElements] elementNameIndexArray] objectAtIndex:section];
}

if i am loading directly from a sqlite3 database into a TableView how could i do the same thing, bubblesort it and arrange it in Section headers
i was thinking you could load that sqlite3 data into an array and sort the array but that doesn't seem to effiecient/as in your loading the same data twice when you already loaded it from the database

any suggestions
thanks
 

wizard

macrumors 68040
May 29, 2003
3,854
571
Study up a bit on SQL

I'm not 100% sure I follow what you are asking here but you might be able to do what you want with the right SQL query.
 

mekopolis

macrumors regular
Original poster
Feb 10, 2008
152
0
basically what i want is some of the user interface design that theElements has,

when scrolling through a table view, the data (from the example, derived from a pre-established Array), and from what i have, an sqlite3 database) is sorted alphabetically with Headers (a,b,c) and to the right there is that lil index so the user can jump to any letter in the alphabet

my data in the sqlite3 database is broken up into things like, PK, name, address

i'll even upload my project if someone is willing to help me tackle this...the code is messy but it works, i've tried to document it

i've been also trying to add a live table search and i think i messed up with the initial design because i want to use a UIscrollView but i think i have to start from a nav controller...anywho thats a whole different problem...

with this header, sorting thing, i would just like to present the data better
 

xnakx

macrumors newbie
Oct 5, 2008
17
0
having not seed your db i can only suggest using the your sql query to sort your result. possibly "SELECT field_name AS Section" can limit the amount of logic you need to get the section names
 

mekopolis

macrumors regular
Original poster
Feb 10, 2008
152
0
currently i do use a "select * from company"
and it retrieves is in the order of the first variable, the PK and displays them in that order on the UItableview
sorting is important, but what i am really looking for is to organize them by state, and display each catagory by state, and have that section header organization like in TheElements to browse by
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.