Julien 'JS'
Julien 'JS'

Reputation: 149

Dynamic number of Sections & Row in TableView

I am a beginner in objective-C and I have a lot of trouble to managing tables ! In this case, I have a table with a variable number of sections, and each section, I have a variable number of rows.

In the method "getTimeslots" I get a JSON that represents a list of appointments. Here is a little piece of my JSON:

data{
         "Sunday, August 10, 2014" = {
            timeslots = (
                            {
                    id = 1013;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 5:15 p.m.;
                    timestart = 5:00 p.m;
                }
                            {
                    id = 1045;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 5:30 p.m.;
                    timestart = 5:15 p.m.;
                }
            );
        };
        "Sunday, July 13, 2014" = {
            timeslots = (
                            {
                    id = 705;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 4:00 P.m;
                    timestart = 3:45 p.m.;
                }
                            {
                    id = 725;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 4:15 p.m.;
                    timestart = 4:00 p.m;
                }
                            {
                    id = 745;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 4:30 p.m
                    timestart = 4:15 p.m.;
                }
                            {
                    id = 1009;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 5:15 p.m.;
                    timestart = 5:00 p.m;
                }
                            {
                    id = 1041;
                    idPatient = "<null>"
                    status = 0;
                    timeEnd = 5:30 p.m.;
                    timestart = 5:15 p.m.;
                }
            );
        }

        AND LOT OF DYNAMICS DAYS AND DYNAMICS APPOINTMENTS....
};

I find how to add in my table "tableTimeslots" one section per day but I don't find how to add appointments associated with these days. I tried many things but nothing could be done :(

This is my code :

@interface AgendaViewController ()
{
    NSMutableArray * sectionsArray;
    NSMutableArray * rowsInSectionsArray;
}
@end

@implementation AgendaViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // Title
    self.title = @"Agenda";

    sectionsArray = [[NSMutableArray alloc]init];
    rowsInSectionsArray = [[NSMutableArray alloc]init];

    [self getTimeslots];

    [[self tableTimeslots]setDelegate:self];
    [[self tableTimeslots]setDataSource:self];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [rowsInSectionsArray count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [sectionsArray objectAtIndex:section];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Cellidentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
    }

    cell.textLabel.text = [rowsInSectionsArray objectAtIndex:indexPath.row];

    return cell;
}

- (void)getTimeslots
{
    NSURL *url = [NSURL URLWithString:getDataURL];
    NSData *allData = [NSData dataWithContentsOfURL:url];

    NSError *error;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:allData options:NSJSONReadingMutableContainers error:&error];

    NSDictionary *data = [jsonObject objectForKey:@"data"];

    for (int i = 0; i < data.count; i++)
    {
        NSArray *days = [data allKeys];

        [sectionsArray addObject:days[i]];

        NSArray *tsArray = [data objectForKey:[days objectAtIndex:i]];
        NSArray *tsList = [tsArray valueForKey:@"timeslots"];

        for (NSDictionary * oneTS in tsList)
        {
            NSString *TSid = [oneTS objectForKey:@"id"];
            NSString *TSstatus = [oneTS objectForKey:@"status"];
            NSString *TSstart = [oneTS objectForKey:@"timeStart"];
            NSString *TSend = [oneTS objectForKey:@"timeEnd"];
            NSString *TSidp = [oneTS objectForKey:@"idPatient"];

For rows, i try this but not work :

            [rowsInSectionsArray addObject:TSid];
            NSArray *newRow = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[rowsInSectionsArray count]-1 inSection:i]];
            [[self tableTimeslots] insertRowsAtIndexPaths:newRow withRowAnimation:UITableViewRowAnimationTop];

        }

    }

    [self.tableTimeslots reloadData];
}


@end

If someone could help me that would be great because I'm lost ! Thanks in advance

Upvotes: 0

Views: 1010

Answers (1)

Cornelius
Cornelius

Reputation: 4264

Your tableview builds its sections and rows from the datasource. The datasource says the tableview should have [sectionsArray count] rows (that works) and [rowsInSectionsArray count] rows.

In getTimeslots you add all timeslots to the same array. This way the tableview thinks all sections should have the same number of rows, which isn't the case. You should have one array of rows per section, best inside another array:

rowsInSectionsArray = [[NSMutableArray alloc] init];

...
NSMutableArray *timeslotsInDay = [[NSMutableArray alloc] init];

for (NSDictionary * oneTS in tsList)
{
    NSString *TSid = [oneTS objectForKey:@"id"];
    [timeslotsInDay addObject:TSid];
}

[rowsInSectionsArray addObject:timeslotsInDay];

Then in your -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section function do

if (rowsInSectionsArray.count > section) {
    return rowsInSectionsArray[section].count;
}

Upvotes: 2

Related Questions