Indexed Tables: Part 1August 02, 2011 10:57 am Posted By: bytedissident
UITableView , Indexed Tables:
Thereʼs a lot you can do with Tables we are going to look at indexed tables and also, reacting to table cell presses. Indexed tables will bring many facets to what you have been studying together and itʼs a very common and useful technique for organizing data. In case you donʼt know what an indexed table is. You see it every time you open your contacts and you get the a-z lettering that helps you quickly navigate your contacts.
To begin with there are 2 delegate methods that are required in order for indexed tables to work:
1.sectionIndexTitlesForTableView
2.titleForHeaderInSection
Before we dive into how this works let me go through how indexing works. Itʼs simple to implement once you understand it, but it pulls in your knowledge of Arrays and how to use them in the real world. The table gets instantiated and 1st UITableView looks for the number of sections in the table, then loops through each section and as it does it looks for the number of cells per section and at that point it populates each Cell. In the case of an indexed table the table will contain as many sections as you have indexes. In the case of an alphabetical index it would have 26 sections. So in order to index a table you would create an Array that would serve as your index array. If you take a look at the sample code I have an array called letters which simply has 26 indexes a-z.
The process that happens for UITableView is as follows:
1. UITableView looks to see how many sections your table contains. in this case it contains [letters count], the number of indexes you are using
2. sectionIndexTitlesForTableView – This methods simply returns the Array you are using for indexing.
3. Then as it loops through each section it hits
4. titleForHeaderInSection. This method returns a string that will serve as the header for that section. The blue band with the letter in it that you are probably used to seeing from other applications
5. Then it hits cellForRowATIndexPath. Here I have put in dummy data. UITableView automatically adds the Index UI for you and your ready to go.
Download Files
|