Separating UITableView data into sections from one source - iphone

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.

Related

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

NSDictionary as a data structure for objects question

I just wanted to make sure I don't need Core Data for a simple example. If I have a UITableView where the user can hit the plus sign to add new table entries, and each entry corresponds to an individual timer event, do I store each object added to the table in an NSDictionary? Thanks.
NSMutableArray would be a better choice. An array is an ordered list, just as a table is. A dictionary is not ordered, so it's not a good match for a table.
If that's confusing, try thinking about it in concrete terms. Imagine that you have a table and you've stored the data for the table in a dictionary. At some point, the table will ask it's data source -- probably your view controller -- to provide a cell for a particular row of the table. How do you figure out which entry of the dictionary corresponds to that cell? On the other hand, if you use an array, the mapping between table rows and data items is simple.

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

NSMutableSet vs NSMutableArray

What's the difference?
In my context, I need to be able to dynamically add to and remove objects. The user clicks on rows of a table that check on and off and thus add or remove the referenced object from the list.
A wild guess is that array has indexed items while set has no indexes?
An NSSet/NSMutableSet doesn't keep items in any particular order. An NSArray/NSMutableArray does store the items in a particular order. If you're building a table view, you should definitely use an array as your data source of choice.
Also, NSMutableSet makes sure that all objects are unique.
NSMutableArray goes well with UITableView since elements have index, so you can return [array count] to get number of table rows, or [array objectAtIndex:rowNumber] to easily associate element with row.
Also, according to the documentation, testing for object membership is faster with NSSets.
You can use sets as an alternative to arrays when the order of
elements isn’t important and performance in testing whether an object
is contained in the set is a consideration—while arrays are ordered,
testing for membership is slower than with sets.
Well there are 3 main differences. 1) Sets are unordered 2)Don't have an index & 3)Cannot have duplicate values where Arrays are ordered, indexed & can store duplicate values.