user3558893
user3558893

Reputation: 11

Create tableView sections dynamically using array in iPhone

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

Upvotes: 0

Views: 1039

Answers (3)

staticVoidMan
staticVoidMan

Reputation: 20274

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;
}

Upvotes: 1

Lithu T.V
Lithu T.V

Reputation: 20021

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

Upvotes: 0

Gyanendra Singh
Gyanendra Singh

Reputation: 1483

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];
}

Upvotes: 0

Related Questions