iphone - Create NSMutableDictionary filled with NSMutableArrays dynamically

I have an array of objects. Each object has property "date" and "title". I want to populate sectioned UITableView with those items like:

Section 1 - 2012.06.12 (taken from object.date)
  Cell 1.1: Title 1 (taken from object.name)
  Cell 1.2: Title 2
  Cell 1.3: Title 3
Section 2 - 2012.06.13
  Cell 2.1: Title 1
  Cell 2.2: Title 2
Section 3 ..

I can do that by manually creating 1..n NSMutableArrays for all date combinations and filling them with object.name values. But the problem is I do not know how many date combinations there are, so it should be done dynamically. Also, the date property can repeat in different objects My object structure is:

 -NSDate - date
 -NSString - title


I was thinking if it is possible to create NSDictionary, where the key would be my date and the object would be NSArray, which contains all my items for the key-date. But I do not know how to do that dynamically.

I hope I explained my question clearly enough. Thank you in advance!


You can create arrays based on date.You have array of objects, so iterate through this array of objects to get distinct dates, as follows:

for(int i =0;i<[objectsArr count];i++)
   if(![newDateArr containsObject:[objectsArr objectAtIndex:i].date])
      [newDateArr addObject:[objectsArr objectAtIndex:i].date];
   NSMutableArray *newTitleArray = [newTitleDictionary objectForKey:@"[objectsArr objectAtIndex:i].date"];
   if(newTitleArray != nil)
      [newTitleArray addObject:[objectsArr objectAtIndex:i].title];
      newTitleArray = [[[NSMutableArray alloc] init] autorelease];
      [newTitleArray addObject:[objectsArr objectAtIndex:i].title];
   [newTitleDictionary setValue:newTitleArray forKey:@"[objectsArr objectAtIndex:i].date"];

where newTitleDictionary and newDateArr are declare outside this method.Now you can use both is newTitleDictionary and newDateArr to populate tableview.

If I understand you correctly, you want to put an object into an array and then use that array to populate a table view?

Just add the date object each time to the NSMutableArray.

[myArray addObject:dateObject];

Then when it comes to populating the table view..

DateObject *newDateObj = [myArray objectAtIndex:index];

I hope this helps and I understood your question

EDIT To answer now I understand a bit more.

Step 1 Check through the existing array of dates and see if there are any that match maybe by iterating through it using a for loop. Search online for how to compare NSDate.

Step 2 If it doesn't match any then insert it into the array as an array with just that date on it's own so the array count will be one. If it does match then insert it into the array along with that one making the array count 2 or more.

Step 3 When it comes to declaring the section amount for the table just return the dateHolderArray count.

Step 4 When declaring the amount of rows in each section, return the array count for the array thats inside the dateHolderArray.

Step 5 Display the content when it comes to populating the cells with information. It becomes just a task of getting the dates from the arrays using the section ids and row ids.

This is how I would do it, there are probably many other methods. Any questions just ask

Need Your Help

Is it possible to use cyrillic symbols in Lumen(by Laravel)?

php laravel utf-8 lumen cyrillic

The issue is I can't use any russian symbols in the response()-&gt;json() method.

Equivalent to VB AndAlso in SQL?

sql sql-server sql-server-2005 tsql short-circuiting

Is there an equivalent to VB's AndAlso/OrElse and C#'s &amp;&amp;/|| in SQL (SQL Server 2005). I am running a select query similar to the following: