2010-04-22 2 views
1

주소록 응용 프로그램과 비슷한 첫 번째 문자로 그룹화 (및 정렬)하려는 이름을 포함하는 UITableView가 있습니다. 나는 현재 어떤 섹션 ('A'- 'Z')을 일치시킬 수입니다 사용 :코코아 터치 UITableView 알파벳 '#'모두 일치하지 않음

// Sections is an array of strings "{search}" and "A" to "Z" and "#". 
NSString *pattern = [self.sections objectAtIndex:section]; 
NSPredicate *predicate = nil; 

// Ignore search pattern. 
if ([pattern isEqualToString:@"{search}"]) return nil; 

// Non-Alpha and Non-Diacritic-Alpha (?). 
if ([pattern isEqualToString:@"#"]); 

// Default case (use case and diacritic insensitivity). 
if (!predicate) predicate = [NSPredicate predicateWithFormat:@"name beginswith[cd] %@", pattern]; 

// Return filtered results. 
return [self.friends filteredArrayUsingPredicate:predicate]; 

그러나, '#'에 대한 일치하는 것은 저를 회피한다. 내가 사용 정규식 일치 구성 시도 :

[NSPredicate predicateWithFormat:@"name matches '[^a-zA-Z].*'"]; 

을하지만 이것은 분음 부호 알파 실패 (중복 행이 나타납니다). 어떤 아이디어라도 대단히 감사하겠습니다! 감사.

답변

6

일반적으로 UILocalizedIndexedCollation 및 맞춤 NSArray 카테고리를 사용합니다. 또한 UILocalizedIndexedCollation 용 래퍼 (데코레이터)가 있는데,이 심볼은 검색 심볼을 포함하고이를위한 오프셋을 처리합니다.

NSArray 카테고리의 구현은 여기에 있습니다 : 그래서 http://gist.github.com/375409

하는 name 속성 객체 objects의 배열을 지정해, 내가 지금과 같은 인덱스 배열을 만들 것입니다 님의

UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; 
NSArray *indexedObjects = [objects indexUsingCollation:collation withSelector:@selector(name)]; // returns an array of arrays 

그것의 중요 UILocalizedIndexedCollation은 이미 지역화 된 방식으로 개체를 인덱싱하고 그룹화하는 논리를 다룹니다. 따라서 여기에 바퀴를 재발 명할 필요는 없습니다.

검색 아이콘을 처리하는 내 데이터 정렬자는 this github gist에서 찾을 수 있습니다.

자세한 사용법은 on my blog입니다.

위의 예에서 UILocalizedIndexedCollation 대신에 내 데이터 정렬 래퍼 인스턴스를 전달하면됩니다.

+0

빠른 답장을 보내 주셔서 감사합니다. 지금 막 모든 것을 살펴보고 작동하는지 확인하기위한 수정 작업을하고 있습니다! 건배. –