NSDictionary as a data structure for objects question - iphone

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.

Related

Objective-C, I'm having some problems with arrays and dictionaries atrting at zero or not?

I'm using a dictionary and an array to store the section title for a table view, along with the number of rows in each. I'm adding to the array and the dictionary in a loop of the records from my database in mu populateDataSource method.
I've got two dates from three records in my database. 2010-11-05 and 2010-11-07
I'm not sure if its because the table view events need to start at zero and the dictionary / array start at one ?
Or maybe my sectionTitles addObject, is added values each time, rather than adding unique dates. If so how i can search or only allow unique values ?
Any ideas ?
Arrays have a starting index of 0, but dictionaries don't essentially work that way. What is the purpose of the dictionary in this case? To get an object from the dictionary you give it a key. If you're going through a loop and not getting the expected result, maybe the dictionary isn't getting the key that it should be.

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.

iPhone:How do i insert few rows in the middle of existing table?

If i want to insert few rows in the middle of the existing table, how do i approach it? Where all that i should be careful when doing such thing? Is there any good sample source available?
Appreciate your helps.
Thank you.
Tableviews don't contain data and they don't actually contain rows themselves. Instead, tableviews are just an illusion created by redisplaying the same tableviewcell objects over and over again with different data each time. The real table, the real rows, are actually in the logical data.
So, to " insert few rows in the middle of the existing table" you actually add the rows into the data that the table displays. For example, if you had a simple array of names that you display in the table, you would insert the new names into the array. Then you would call -[UITableView reload] which will cause the table to ask the datasource for the new data.
See the Table View Programing Guide for iPhone OS.
Insert the data into your model and call reloadData on the table. If you need to animate the insertion for some reason, you can use insertRowsAtIndexPaths:withRowAnimation:, but the principle if updating the model first still applies.

how to collect the data from database in array and then put that data for each row of table view in objective-c

i want to collect the data from database in the array and then after put that in each cell of the table view.
i dont know how to retrieve the data in array for select * from tablename for single row.
If you're talking about a Core Data database, doing this tutorial from developer.apple.com should give you an idea of how to do it.
Sam