Problem with showing Grouped Data in UITableView

Discussion in 'iOS Programming' started by Narendar Singh, Jul 16, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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.
     

    Attached Files:

  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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?
     
  3. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #3
    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.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    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?
     
  5. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #5
    hmmm... not much, but how to get no of sections and no of rows in each section from my array?
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    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.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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.
     
  8. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #8
    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];
        }
     
  9. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #9
    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.
     

Share This Page