views:

55

answers:

1

I need to create a table view with 1 or 2 sections, depending on certain conditions. The first section needs to contain all of the remaining months of the current year, and the second section contains the preceding months of the next year, up to but not including the current month.

Example:

2009
  November
  December

2010
  January
  February
  March
  April
  May
  June
  July
  August
  September
  October

This would be the scenario with the current month of November. However, if it were January, there would be only 1 section containing all 12 months of the current year.

This all needs to be dependent on the date settings of the phone.

A: 

I actually got it working...whether or not it's the best method is open for discussion. Here is the code I came up with:

- (void)viewDidLoad {
[super viewDidLoad];

NSArray *monthArray = [NSArray arrayWithObjects:@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];

NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];

currentYear = [NSString stringWithFormat:@"%d", year];
nextYear = [NSString stringWithFormat:@"%d", year+1]; 


[dateComponents setMonth:month];

currentYearMonths = [[NSMutableArray alloc] init];
nextYearsMonths = [[NSMutableArray alloc] init];

for(uint i=month-1; i<=11; i++){
 [currentYearMonths addObject:[monthArray objectAtIndex:i]];
}
for(uint i=0; i<month-1; i++){
 [nextYearsMonths addObject:[monthArray objectAtIndex:i]];
}

monthList = [[NSArray alloc] initWithArray:monthArray];

[calendar release];
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if([nextYearsMonths count] == 0)
     return 1;
    else
     return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section == 0){
     return currentYear;
    }else if(section == 1){
     return nextYear;
    }
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
     return [currentYearMonths count];
    if(section == 1)
     return [nextYearsMonths count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int section = [indexPath indexAtPosition:0];

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    int monthIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    switch (section) {
        case 0:   
      if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }   
      cell.textLabel.text = [currentYearMonths objectAtIndex:monthIndex];
            break;

        case 1:
      if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }   
      cell.textLabel.text = [nextYearsMonths objectAtIndex:monthIndex];
            break;
    } 
    return cell;
}
rson
So you have it working just 2 hours later. Good work, you really can do it yourself, although it may take a while (and 2 hours isn't really that much). One thing: initialize the cell just after you dequeue, that will save you a lot of copying in the switch.
JoostK