Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
I have JSON like below

{
createdDate = "Sunday, November 27";
imageURL = "www.abc.com/john.png";
imageName = "John"
},
{
createdDate = "Sunday, November 27";
imageURL = "www.abc.com/sam.png";
imageName = "Sam"
},
{
createdDate = "Saturday, November 19";
imageURL = "www.abc.com/jubin.png";
imageName = "Jubin"
},
{
createdDate = "Saturday, November 19";
imageURL = "www.abc.com/vicki.png";
imageName = "Vicki"
},

I have to display this data in Table View Grouped by date ie date 27 has 2 items and similar date 19 has another 2 items, how can I achieve this using NSArray and NSDictionary or any other data structure.

Please see the attached image for your reference.

My Table View understanding is
1) First we need to count no of sections
2) Then no rows in each sections
3) Then we can set height for section if required
4) also setting the header for each section
5) rendering cell for each row in a section

But I was not able to show this JSON data in table view. Any hint or idea would be appreciable.
 

Attachments

  • Grouped Data.png
    Grouped Data.png
    15.4 KB · Views: 157

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Basically, you need an array with all the sections = that would be all your dates :)
Then for each section it will render a cell, so have seperate dictionary/array to loop over in the cell for row at index (you can check from which section).
I don't really fully understand what you exactly want ^_-
Since the screenshot looks fine?
 

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
I don't really fully understand what you exactly want ^_-
Since the screenshot looks fine?

Actually after receiving above JSON response from the Server, I am storing this in NSArray ie

Code:
NSArray *response = [resultFromServer valueForKey:@"myData"];

for(int i = 0; i< [response count]; i++)
{
   // here I am able to traverse all the rows; and columns in each row
}
then struck how to render this data on grouped table ie date wise.
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
I see, the thing is, I would recommand in a MVC pattern that you create your own NSObjects, for example.
ListItem (NSObject), with property's, createDate,imageURL & imageName.
Store them as ListItems in your array and then show that info in the cell..
So basically in the cell:
Code:
ListItem *item = [self.yourarray objectAtIndex:indexpath.row];
cell.textLabel.text = item.imageName;

You understand anything?
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
If you don't listen, you're not gonna work it out..
You need to divide your objects into a dictionary with arrays.. and use the index section number from the cell In row to get the array from the dictionary, but apparently, your knowledge isn't that advanced and you want copy paste code. so i'm out.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
You need to divide your objects into a dictionary with arrays.. and use the index section number from the cell In row to get the array from the dictionary...

I'd probably model this as an array of arrays of dictionaries or custom objects. The top array is for the sections, the next array is for the rows within each section, and the object is for the details of each row.
 

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
Here is my code snippet.

Code:
// "response" has my actual data (already mentioned in my 1st thread)

Code:
// =====================================
// listOfDates will have unique dates ie no of section (descending order)
// =====================================

    NSMutableArray *listOfDates = [[NSMutableArray alloc] init];
    NSString *preDate = [[response objectAtIndex:[response count]-1] valueForKey:@"createDate"];
    NSString *nextDate;
    [listOfDates addObject:preDate];
    
    for (int i=[response count]-1; i>0; i--) 
    {
        nextDate = [[response objectAtIndex:i] valueForKey:@"createDate"];
        
        if ([preDate isEqualToString:nextDate]) 
        {
            preDate = nextDate;
            nextDate = [[response objectAtIndex:i] valueForKey:@"createDate"];
            continue;
        }
        
        preDate = nextDate;
        nextDate = [[response objectAtIndex:i] valueForKey:@"createDate"];
        
        [listOfDates addObject:nextDate];
    }
    DebugLog(@"No of sections %d", [listOfDates count]);

Code:
// =====================
// Creating an array of array
// =====================

    NSMutableArray *list = [[NSMutableArray alloc] init];
    
    for (NSString *date in listOfDates) 
    {
        NSMutableArray *sectionRows = [[NSMutableArray alloc] init];
        
        for (int i=[response count]-1; i>=0 ; i--) 
        {
            if ([[[response objectAtIndex:i] valueForKey:@"createDate"] isEqualToString:date]) 
            {
                [sectionRows addObject:[response objectAtIndex:i]];
            }
        }
        [list addObject:sectionRows];
        [sectionRows release];
    }
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
I'd probably model this as an array of arrays of dictionaries or custom objects. The top array is for the sections, the next array is for the rows within each section, and the object is for the details of each row.

If the data is already Time Sorted then couldn't you have just two arrays.
One of the Data and the second of the offset at which that date starts in the first array.

number of section equals count of the offset array.
number of items in section x equals offset of [x-1]-[x].
data array index for indexpath equals (offset of section) + row.

although working out the indexpath for a given data array index is a little tricker. and keeping it updated might be to much fun.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.