Sorting and Section headers, array or database?

Discussion in 'iOS Programming' started by mekopolis, Oct 18, 2008.

  1. mekopolis macrumors regular

    Joined:
    Feb 10, 2008
    #1
    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
     
  2. wizard macrumors 68040

    Joined:
    May 29, 2003
    #2
    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.
     
  3. mekopolis thread starter macrumors regular

    Joined:
    Feb 10, 2008
    #3
    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
     
  4. xnakx macrumors newbie

    Joined:
    Oct 5, 2008
    #4
    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
     
  5. mekopolis thread starter macrumors regular

    Joined:
    Feb 10, 2008
    #5
    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
     
  6. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #6
    Can't you add "SORT BY state DESC" or ASC to the end of your SQL query?
     
  7. fenrus110 macrumors regular

    Joined:
    Mar 24, 2008
    #7
    I believe it is ORDER BY if sqlite uses standard sql notation.
     

Share This Page