2013-10-15 2 views
0

나는 내 테이블 뷰를 모두 설정 했으므로 알파벳순으로 섹션 인덱스를 사용하여 행으로 이동할 수 있지만 테이블 뷰에는 장치의 연락처 목록과 같은 제목이 있습니다. 예를 들어 :테이블 뷰 제목 인덱스

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 

NSInteger newRow = [self indexForFirstChar:title inArray:self.Make]; 
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRow inSection:0]; 
[tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

return index; 
} 

답변

0

당신은 쉽게 TLIndexPathTools에서 TLIndexPathDataModel를 사용하여이 작업을 수행 할 수 있습니다

다음
A 
Aeroplane 

B 
Bike 
Bus 

C 
Car 

는 내가 지금까지 가지고있는 것입니다. 당신은 각각의 첫 글자에 따라 섹션으로 항목을 구성하는 블록 기반 초기화를 사용할 수 있도록 :

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return [self.dataModel sectionNames]; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    return [self.dataModel sectionForSectionName:title]; 
} 

그리고 다른 :

NSArray *makes = ...; // array of makes 
TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:makes sectionNameBlock:^NSString *(id item) { 
    // assuming the items are NSStrings 
    return [((NSString *)item) substringToIndex:1]; 
} identifierBlock:nil]; 

이 그런 다음 색인 데이터 소스 방법의 모습 데이터 소스 및 위임 방법은 데이터 모델 API에서 간단하게 할 수는 있지만

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.dataModel.numberOfSections; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.dataModel numberOfRowsInSection:section]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellId = ...; 
    UITableViewCell *cell = ...; // dequeue cell 
    NSString *make = [self.dataModel itemAtIndexPath:indexPath]; 
    ... // configure cell 
    return cell; 
} 

당신은합니다 ("Blocks" sample project을 실행하여 작업 예제를 볼 수 있습니다 일부 추가 TLIndexPathTools 구성 요소 (TLTableViewControllerTLIndexPathController)를 사용합니다.