2009-11-18 34 views
0

我需要根據特定條件創建1或2個部分的表格視圖。第一部分需要包含當年的所有剩餘月份,第二部分包含下一年的前幾個月,直到但不包括當前月份。Month TableView Organizer

例子:

2009 
    November 
    December 

2010 
    January 
    February 
    March 
    April 
    May 
    June 
    July 
    August 
    September 
    October 

這將與11月當月的情況。但是,如果是1月份,則只有1個部分包含當年的所有12個月。

這一切都需要依賴於手機的日期設置。

+3

和問題? – ennuikiller

+0

我該怎麼做? – rson

+2

你有什麼想法?作爲一名開發人員,您必須分析問題並解決問題,而不是要求馬上做些事情。你是開發者,我們至少希望你嘗試一下。告訴我們你已經嘗試了什麼,以及你面臨的問題。 – Joost

回答

0

我確實得到它的工作......是否最好的方法是開放討論。這是我想出的代碼:

- (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; 
} 
+1

所以你只需要2個小時就可以工作。好的工作,你真的可以自己做,雖然它可能需要一段時間(2小時不是那麼多)。有一件事:在你出隊後立即初始化單元,這將爲你節省很多在交換機上的複製。 – Joost