0

I have app in which I want to show the data in table view sections problem is that I do not know the total sections names as it depends on array. So how to show data for each section in cell for row at index path.

NSArray *test=[NSArray arrayWithObjects:@"June 20",@"July 20","May 1",@"May 10",nil];

So I want to show the data in the tableView sections date wise like if June 20 all the records of June 20 should be shown in one sections and in May 1 all the records for May same goes for all. Any idea how to solve this. Thanks

3
  • 1
    so test is the section name but from where will you be displaying the data for the respective dates? if you've haven't reached there yet, you could consider using an carefully structured array of dictionaries and arrays. Commented Apr 22, 2014 at 4:52
  • @staticVoidMan can you please share me some help or link how to do that thanks Commented Apr 22, 2014 at 4:56
  • ok, i answered and i hope it helps you get an idea Commented Apr 22, 2014 at 5:51

3 Answers 3

1

It's best to create a standard structure like (personally, i'd suggest the following):

  1. Array that contains multiple dictionaries
  2. Each dictionary contains 2 keys
    • Day is a key and, say, June 20, is it's value
    • Events is a key and, it's value is an array object
      • This array is a collection of strings that will basically be the content for these days

Example:

Possible UITableViewController subclass methods

- (void)viewDidLoad
{
    [super viewDidLoad];

    arrTest = @[
                @{@"Day":@"June 20",
                  @"Events":@[@"Repair window pane", @"Buy food for Elephant", @"Pay credit card dues"]},
                @{@"Day":@"July 20",
                  @"Events":@[@"Repair window frame", @"Buy alot more food for Elephant", @"Pay credit card dues dues"]},
                @{@"Day":@"May 1",
                  @"Events":@[@"Replace entire window", @"Sell Elephant, Get Cat", @"Return credit card"]},
                @{@"Day":@"May 10",
                  @"Events":@[@"Take bath", @"Shave", @"Get new credit card"]}
                ];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //get count of main array (basically how many days)
    return arrTest.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //get count of number of events in a particular day

    //old style
    //return [[[arrTest objectAtIndex:section] objectForKey:@"Events"] count];

    return [arrTest[section][@"Events"] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //get value of the "Day" key (June 20 / July 20 ...)

    //old style
    //return [[arrTest objectAtIndex:section] objectForKey:@"Day"];

    return arrTest[section][@"Day"];
}

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

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

    //old style
    //NSString *strDayEvent = [[[arrTest objectAtIndex:indexPath.section] objectForKey:@"Events"] objectAtIndex:indexPath.row];

    NSString *strDayEvent = arrTest[indexPath.section][@"Events"][indexPath.row];
    [cell.textLabel setText:strDayEvent];

    return cell;
}
Sign up to request clarification or add additional context in comments.

Comments

0

To decide number of section and header title for particular section you need to do implement UITableView delegates methods.

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

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

return [test objectAtIndex:section];
}

1 Comment

How are you storing the records for the particular month?You need to create a Array of Dictionary (key will be month and values will be an array having all the records in same month). In terms of JSON something like this [ { "June 20": { "record_name": "xyz", "record_id": 1 } }, { "June 30": { "record_name": "abc", "record_id": 2 } } ]
0

The section name and the row contents have a relationship...and therefore the most suitable way is to manage it with objects displaying relationship like the requirement

For your case an object/structured array will do the job for you with object has a string content to store the date text and the array storing the cell content data

Or use an NSDictionary with key as date and value as the cell content data as array

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.