2009-11-18 4 views
0

특정 조건에 따라 1 개 또는 2 개의 섹션이있는 테이블보기를 만들어야합니다. 첫 번째 섹션은 현재 연도의 남은 달을 모두 포함해야하고 두 번째 섹션은 다음 달의 전월을 포함합니다 (단, 현재 달까지 포함).월 TableView 구성 도우미

예 :

2009 
    November 
    December 

2010 
    January 
    February 
    March 
    April 
    May 
    June 
    July 
    August 
    September 
    October 

이 11 월 현재 월과 시나리오가 될 것입니다. 그러나 1 월이면 올해의 모든 12 개월을 포함하는 섹션이 1 개만 존재하게됩니다.

이 모든 것은 휴대 전화의 날짜 설정에 따라 달라질 필요가 있습니다.

+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