0

I've already got an array of dictionary (for loan items), example below:

(
    {
    BIBNo = 291054;
    date = "10/08/2012";
    itemSequence = 000010;
    name = "iPhone application development for IOS 4 / Duncan Campbell.";
    noOfRenewal = 1;
    number = 000291054;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
},
    {
    BIBNo = 187883;
    date = "13/08/2012";
    itemSequence = 000010;
    name = "Positive thinking / Susan Quilliam.";
    noOfRenewal = 2;
    number = 000187899;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
},
    {
    BIBNo = 290408;
    date = "13/08/2012";
    itemSequence = 000010;
    name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
    noOfRenewal = 1;
    number = 000290408;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
})

And right now I need to insert them into tables, with date as the section and rows classified into section accordingly to the date. I've gone through many tutorials but however, most of the tutorials just uses static hardcode of the sections and rows which looks something like that:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0){
    return 1;
}
else if (section == 1){
    return 5;
} }

Because the loan items array is dynamic, therefore i cannot hardcode the rows nor the sections to return certain values. Anyone can highlight to me how should I carry on so that I can return dynamic values? If possible, examples would be appreciated! Thanks all :)

4
  • Then you need to count your dictionary how many different date and how many dictionaries with the same date right? Commented Jul 25, 2012 at 8:12
  • Yup that's right. Any idea how to do that? What I did was that i created another array with the same loan data and i did some comparison and removed the similar dates. Therefore i could still get the count of the section and title for section Commented Jul 25, 2012 at 8:15
  • however, as it came to the rows, i'm lost at how to return the number of dynamic rows. Commented Jul 25, 2012 at 8:15
  • @Esses77 's answer is clear. You can try to implement it:) Commented Jul 25, 2012 at 8:17

3 Answers 3

1

In such a case i coded a NSOperation that filters items by date ( a dictionary whose key is the date and each value is an array of items) then you will obtain sections by getting dictionary allkeys array and for each section you will get the row array using objectForKey and the date.

To filter you can use many methods like using NSPredicate ...

When the NSOperation finished and the code receive the notification that the dictionary is ready you call reloadData and you have the table organized as you want.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm, if possible could you add in some code examples for me to understand better? It's the first time i'm seeing NSPredicate..
And, how should I return the count after flitering?
Hi, you can remove array dates strings dupicates putting the array in a nsset and then getting the array again calling allObjects method of nsset so: NSSet *dateSet = [NSSet setWithArray: dateArr]; [dateArr setArray: [NSMutableArray arrayWithArray: [dateSet allObjects]]];
I'm sorry for the formatting but I'm using iPad and when i press enter i cannot go down but send the comment...
0

The following code is for your reference. here the array is the array you posted, mutDic is the dictionary mentioned by @Esses77

NSArray *array;

NSMutableArray *dateArr = [NSMutableArray array];

// dateArr is used to collect all the date in your array
for (NSDictionary *dic in array) {
    [dateArr addObject:[dic valueForKey:@"date"]];
}


//the following code is used to remove all the duplicate date
NSArray *copy = [dateArr copy];

NSInteger index = [copy count] - 1;

for (id object in [copy reverseObjectEnumerator])
{

    if ([dateArr indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound)

    {
        [dateArr removeObjectAtIndex:index];

    }

    index--;
}

//group all the dictionaries with the same date in mutDic. and key is the date, object is the array of dictionaries with the same date
NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];

for (NSString *date in dateArr) {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date like %@", date];

    [mutDic setValue:[array filteredArrayUsingPredicate:predicate] forKey:date];

 }

The number of your sections is the number of keys in mutDic and the number of rows is the corresponding object(it's an array).count.

5 Comments

My pleasure:). I just wrote the code, and do not test it. But the principle is clear.
I've tried and there's an error T_T *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x628dd60> was mutated while being enumerated.
it's okay I think i just experiment around and post the answer up later on. Still thankful! :)
@XUESNOW debug it. Have you tried to set some break point and NSLog the object? It seems it crashed in the "[copy reverseObjectEnumerator]" forin loop.
@XUESNOW Take a look at the link stackoverflow.com/questions/6375409/…
0

I am supposing that you have an Mutable Array of Dictionary according to the data displayed above in your questiong for example : arrayOfMydata

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 8;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
 {
     return [arrayOfMydata count];

 }


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

     NSDictionary *tempDictionary  =  [arrayOfMydata objectAtIndex:section];
     NSString *sectionString       =  [tempDictionary objectForKey:@"date"];
     return sectionString;
 }

Hope this will help you

6 Comments

By this with the help of only one array i.e arrayOfMydata you can also easly populate your tablecell and can reload the table anytime by altering your array i.e arrayOfMydata.
in numberOfRowsInSection, if i return [arrayOfMydata count], what happens is that it would return the count of all the objects in the array instead of just returning the objects with key @"date" that matches the section. For example, in the it would return 3 section with 3 rows.
What i need is to return 2 section (10/08/2012 and 13/08/2012), 1 row under 10/08/2012 and 2 rows under 13/08/2012
@XUESNOW sorry for that... numberofrows in a section will be constant if the data of your dictionary is not changing.. in every dictionary there are 9 fields out of which one is date which you want to show as header of section so for numberofrows in a section you can hardcode 8
There is one more solution to this NSDictionary *dict = [arrayOfMydata objectAtIndex:section]; return [[dict keys] count] -1; this will give you a dynamic solution no matter number of objects inside dictionary it will give one less count..the reson i m using one less count because of date if you want to show date also dont do -1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.