iPhone UITableView populating variable rows sections from flat array - iphone

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

Related

In What Order UITableView sections are displayed?

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);

Shifting rows in a UITableView

I have looked at several forums and read different threads, but I can't seem to find an answer. I'd really like some help. I have an array that stores Labels whose contents I display in the UITableView's row. The second row contains fresh data added to the same array and so on. My question is, everytime the labels are displayed in the UITableView, I want it to be displayed on the top row. In essence, the older ones keeps shifting one row down. I understand I need to use: "tableView:moveRowAtIndexPath:toIndexPath:" but it only seems to work with arrays whose contents are strings.
Just to clarify, I'm not displaying each element of the array on a new line. I'm displaying all the elements of the array on a single row, then changing the elements and displaying that as the next row. Except I want the second row to be above the first and so on.
The array you're using is an ordered collection of object, so the easiest way to perform what you're looking fro would be to add the new object in the first position of your array. You can achieve that using the method insertObject:atIndex: of NSMutableArray
ex:
[myArray insertObject:newObject atIndex:0];
This way you don't have to worry about moving your tableView cells ordering.
Hope this helps,
Vincent
First of all you don't need to keep the label in some array, you can keep only the string. And when add an new object to the array add it on position 0 and the do and reload of table view.

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.

Separating UITableView data into sections from one source

I have one table in my core data source which holds some articles with NSDate's. I basically want to separate the NSManagedObject into days, so you can scroll through and separate articles by date.
What is the best way to approach this? My context is queried out in descending date order, so just need to split that up into days for the UITableView sections, rather than 1 big section.
When you load the data for your table -- whether that's using NSFetchedResultsController and Core Data, or loading a .plist file into an NSDictionary object -- you can "section" it as you like.
You'll first step through the data (e.g. use the fetchedObjects property if using NSFetchedResultsController) and determine what sections you want. Since you're wanting to split on dates, you might store cutoffs represented by NSDate objetcts in an array. You would then use this array to implement the various UITableViewDataSource methods: in numberOfSectionsInTableView: you can return the count of this array, and in sectionIndexTitlesForTableView: you can return NSString representations of those stored NSDates for the section titles.
Methods like tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: are a little trickier. You'll probably want to use another "preprocessed" data structure (perhaps a two-level array of arrays, where the inner arrays contain the entity objects you fetched), and you'll want to set up as the same time as your array of NSDate objects.

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.