2012-10-30 5 views
0

테이블 뷰가 날짜로 채워져 있습니다. 내 섹션 헤더는 월 이름입니다. here 이상의 내 tableview를 볼 수 있습니다.테이블 뷰의 특정 포인트로 스크롤

내가 원하는 것은 그 순간의 해당 부분으로 스크롤된다는 것입니다. 내 섹션 헤더를 설정하기 위해이 메소드를 사용한다.

id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section]; 

     static NSArray *monthSymbols = nil; 
     NSArray *dutchMonths = [[NSArray alloc]initWithObjects:@"Januari",@"Februari",@"Maart",@"April",@"Mei",@"Juni",@"Juli",@"Augustus",@"September",@"Oktober",@"November",@"December", nil]; 
     if (!monthSymbols) { 
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
      [formatter setCalendar:[NSCalendar currentCalendar]]; 
      [formatter setMonthSymbols:dutchMonths]; 
      monthSymbols = [formatter monthSymbols]; 

     } 
     NSLog(@"%@",monthSymbols); 
     NSInteger numericSection = [[theSection name] integerValue]; 

     NSInteger year = numericSection/1000; 
     NSInteger month = numericSection - (year * 1000); 

     NSString *titleString = [NSString stringWithFormat:@"%@", [monthSymbols objectAtIndex:month-1]]; 
     label.text = titleString; 

나는이 방법을 사용해야한다는 것을 이미 알고있다.

[sampleListTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

그러나 올바른 행의 색인 경로를 얻으려면 어떻게해야합니까?

어떤 도움이 필요합니까? 더 자세한 정보가 필요하면. 도와주세요.

친절하게 제공합니다.

+0

[self.fetchedResultsController indexPathForObject : object]는 모든 셀에 대한 indexpath를 찾는 방법입니다. – Sandeep

답변

0

섹션 배열 (self.fetchedResultsController에서)을 반복하고 문제의 개체에서 얻은 월을 현재 날짜의 달과 비교하여 색인을 만들 수있는 것처럼 보입니다. 당신이 일치하는 경우, 다음 indexPath은 다음과 같습니다가 그 메소드가 호출 될 때마다 생성되지 않도록 당신은 또한 dutchMonths 배열이 정적해야

NSIndexPath *path = [NSIndexath indexPathForRow:0 inSection:foundIndex]; 

. 또한 ARC를 사용하지 않는 경우 누출이 발생합니다 (릴리스 코드가 게시되지 않은 경우 제외). 일반적인 경험 법칙은 값 비싼 연산이기 때문에 날짜 포맷터를 정적으로 만들거나 어떤 식 으로든 하나의 인스턴스를 관리하는 것입니다. 이 코드를 사용하면 monthSymbols 배열을 채우기 위해 사용하기 때문에 한 번만 생성되지만, 다른 코드에서 동일한 포맷터를 사용해야하는 경우 다시 작성해야 할 것입니다.

이 모든 작업을 수행하려면, 당신은 그럼 당신이 쓸 수있는이 방법의 논리를 추출하고

- (NSInteger)monthFromSection:(id<NSFetchedResultsSectionInfo>)section; 

처럼 작은, 재사용 방법에 넣어해야합니다

NSIndexPath *path = nil; 
NSInteger currentMonth = // Calculate month from date returned by [NSDate date] 
NSArray *sections = [self.fetchedResultsController sections]; 
for (int i = 0; i < sections.count; i++) 
{ 
    if (currentMonth = [self monthFromSection:[sections objectAtIndex:i]]) 
    { 
     path = [NSIndexPath indexPathForRow:0 inSection:i]; 
     break; 
    } 
} 
+0

이것은 나를 도와 줬다! 고맙습니다 –

관련 문제