In What Order UITableView sections are displayed? - iphone

I am getting data from a sqlite3 database and i'm saving those things in an array and i'm using that array in viewForHeaderInSection method, every time my data will be different i mean section list will be different every time it is displaying randomly i need to display it in an order.
1. How a UITableView displays its sections(any sorting algorithm is there) or randomly it will display?
2. How can i sort my sections in an order?
Any help is thankful in advance.

The table view displays its sections in the order you tell it to. If you are populating an array for the sections from a SQL query, you can apply ordering in the query. If this is not possible, you can sort the array after population using one of the many sorting methods available on NSArray, for example sortedArrayUsingDescriptors:. See here for more:
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingDescriptors:

Question is a bit unclear. Better if you can post some code. You get different data from sqlite each time into array and that data is shown in sections. So why do you need to sort sections ? Instead, sort the array according to requirement and show the sorted data in section.

Are you asking about the order of the sections or the order of the content within the sections? For example:
Section 1
Section 3
Section 2
OR
Section 1
Row 1
Row 3
Row 2
Only the order of the Rows is determined by the order of the datasource of the uitableview.

i think you are getting different unordered data form sqlite database into array.you want sort the array(arrList)data
arrList = [arrList sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"%#",arrList);

Related

Explanation on how UITableView's grouped sections and their titleForHeaderInSection work?

So I never GOT how titleForHeaderInSection and, basically, grouped UITableView work. I gave up trying to create one and ended up using plain lists for the moment. I'd appreciate if any of the following points could be explained:
Understanding it...
If I want a grouped UITableView I understand I need to return how many groups (sections) I want in it using numberOfSectionsInTableView. Say, return 3.
Then I'll need to return how many rows I'll have in each different section, right? I can do it in numberOfRowsInSection. Say, return 2 for the first section, 4 for the second, 6 for the third, right?
If I want each of those sections to have a different title, I'll need to use titleForHeaderInSection, but I find it tricky to use. However it seems I just need to get the section integer and set the title to my sections' array using objectAtIndex, right?
In cellForRowAtIndexPath do I need to do anything at all regarding sections or how I'll show the cells? Seems I don't, but I'm not sure...
Problem
My data is structured like that (it's a JSON array containing dictionaries in each array entry):
ArrayItemOne
DictionaryKeyOne:DictionaryValueOne
DictionaryKeyTwo:DictionaryValueTwo
ArrayItemTwo
DictionaryKeyOne:DictionaryValueOne
DictionaryKeyTwo:DictionaryValueTwo
ArrayItemThree
DictionaryKeyOne:DictionaryValueOne
DictionaryKeyTwo:DictionaryValueTwo
My sections titles are actually the DictionaryValueTwo of all 3 ArrayItemFoo above, and it's dynamic, I don't know how many of them will be today or tomorrow, it comes from the JSON. Then I create a unique set of them and count it. That's my numberOfSectionsInTableView.
But how in hell I'll get my rows grouped by this DictionaryValueTwo in each different section, ONLY if their DictionaryValueTwo matches the section header? Right now I can't control what rows will be in each section, it's a mess and I'm confused.
Picture this: all events have a date, my sections are the non-duplicate events dates, but my events need to show up only in the sections matching their dates. That's it. I'm too stupid to understand how grouped UITableView works to do it...
My code
I have coded a UITableView using plain lists already, it has some commented out stuff for grouped lists, if it helps to understand my question: http://github.com/caio1982/CorreCuritiba/blob/master/iOS/CorreCuritiba/CorreCuritiba/CCMasterViewController.m
Again: some code exemplifying the explanations would be great but it's not mandatory, I really am looking to understand grouped UITableViews, the concept, the mechanism.
And yes, I've searched all over and didn't find anyone with the same problem I'm facing.
This is helpful.
IPhone JSON to TableView
Trick is to see each array item as one section.
Gustavo Boiko's patches sort of explains well enough how it's supposed to work. My data structure was wrong after all: https://github.com/caio1982/CorreCuritiba/pull/3

How do I rearrange my UITableView with respect to alphabets or with respect to modified date

I have two table views displaying the data from a Sqlite database.
I can modify the content of the 2 tables. One displayed date and data. One displayed data only.
I.e. I can change date & time,words as well.
Now I need to arrange the content with respect to date in 1 table,
with respect to alphabets in 2nd table.
What to do?
You can use a sortDescriptor to sort the array (or fetchRequest when using Core Data) you're using. If you've setup your tableViews any other way please provide some more information.
Documentation: SortDescriptors
Once sorted you can reload your tableView: [tableView reloadData];
You must be storing your data in some data structure (like array, dictionary etc.), once you edit the date and time, Sort your array or dictionary using sort descriptors and then reload the table

Dynamic Sections UITableView with Unknown cells

Hey,
I'm basically trying to retrieve data from SQlite db and populate a tableView from it.
The sql-data-retrieval method creates two arrays. "dataArray" and "SectionArray".
DataArray is filled with data NSObjects.
Uptil this point, i can create the appropriate Section headers.
Here is the problem,
-1 What do i do to make sure that the right objects get into their appropriate sections and not under any other sections (which they seem to be doing). Also the Count(number of rows) in each section differs.
What should the code be in "NumberOfRowsAtIndexPath" and cellForRowAtIndexPath methods
-2 What kind of datasource objects are more suited for this type. I'm simply filling up two NSMutableArrays - dataArray(rows) and SectionArray(Section headers).
I think you should make many NSArray one for each table header you have created. In NumberOfRowsAtIndexPath you will return the count of the array for the requested section, and in cellForRowAtIndexPath you will choose your array using the section index (as before) and with the row index you will select the row of that array.

iPhone UITableView populating variable rows sections from flat array

I thought that would be very common and easy iPhone App. In the main app I connect to database, retrieve values from database (NSDate converted to NSString)n and put into single array. Then in one of the views I populate UITableView with elements from the array. UITableView is grouped (sections). I step through array to discover number of sections (change section if new day). How do I retrieve correct element of array in cellForRowAtIndexPath? IndexPath.section and IndexPath.row seem useless as row starts count from zero for each section. If number of rows in each section was the same it would have been easy:
[arryData objectAtIndex:(indexPath.row)+indexPath.section*[tblMatchesView numberOfRowsInSection:indexPath.section]];
But number of rows in each section varies... :-)
Separate your data into arrays of arrays (based on the number of different days) once you get it from the database, that'd be the simplest solution...
How about storing different date sections in different arrays? For example, you can have an array A of array. You can loop through the original arrays yourself, if you found a new day, you just create a new array and put it into the array A. And then, when you loop over the cell, you can get the section number to get the correct array and based on the row number to get the correct elemenet in the array
It doesn't have absolute cursor but you can try utilizing
UILocalizedIndexedCollation class which is used to ease the "sectioning" of your data and proving the tableView delegate functions the required data such as the index titles, etc
Here's apple documentation link for it:
https://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalizedIndexedCollation_Class/UILocalizedIndexedCollation.html#//apple_ref/occ/cl/UILocalizedIndexedCollation

Is it possible to get array of all index paths for the cells in a UITableView?

Is there a simple way to get array of all index paths for the cells in a UITableView?
Since you are the one that told the table view how many sections and rows per section you had, you can use that same data - or in fact even call the same methods on your data source to ask that question from elsewhere.