PDA

View Full Version : Sorting and Section headers, array or database?




mekopolis
Oct 18, 2008, 02:02 PM
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)




- (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
Oct 18, 2008, 02:16 PM
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
Oct 18, 2008, 02:44 PM
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
Oct 18, 2008, 03:40 PM
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
Oct 22, 2008, 06:39 PM
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

Aranince
Oct 22, 2008, 06:44 PM
Can't you add "SORT BY state DESC" or ASC to the end of your SQL query?

fenrus110
Oct 22, 2008, 06:48 PM
I believe it is ORDER BY if sqlite uses standard sql notation.