이 문제를 해결하는 가장 좋은 방법은 프레임 워크에서 제공하는 도구를 사용하는 것입니다. 이 경우 UILocalizedIndexedCollation (개발자 링크)을 사용하고 싶습니다.
또한이 클래스의 데코레이터는 {{검색}} 아이콘을 삽입하고 오프셋을 처리하도록 설계되었습니다. UILocalizedIndexedCollation에 대한 드롭 인 대체와 비슷합니다.
이 사용 방법에 대한 자세한 설명은 on my blog입니다. 장식자는 here (요지) 가능합니다.
기본적인 아이디어는 각 배열이 섹션을 나타내는 배열을 배열로 그룹화하는 것입니다. 이 작업을 수행하려면 UILocalizedIndexedCollation
(또는 제 교체 가능)을 사용할 수 있습니다. 여기에 내가이 수행하는 데 사용하는 작은 NSArray
카테고리 방법입니다 :
그래서
@implementation NSArray (Indexing)
- (NSArray *)indexUsingCollation:(UILocalizedIndexedCollation *)collation withSelector:(SEL)selector;
{
NSMutableArray *indexedCollection;
NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];
indexedCollection = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[indexedCollection addObject:array];
[array release];
}
// Segregate the data into the appropriate section
for (id object in self) {
NSInteger sectionNumber = [collation sectionForObject:object collationStringSelector:selector];
[[indexedCollection objectAtIndex:sectionNumber] addObject:object];
}
// Now that all the data's in place, each section array needs to be sorted.
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *arrayForSection = [indexedCollection objectAtIndex:index];
NSArray *sortedArray = [collation sortedArrayFromArray:arrayForSection collationStringSelector:selector];
[indexedCollection replaceObjectAtIndex:index withObject:sortedArray];
}
NSArray *immutableCollection = [indexedCollection copy];
[indexedCollection release];
return [immutableCollection autorelease];
}
@end
, 나는 그들의 이름에 따라 섹션으로 나눌 예를 books
에 대한 객체의 배열, 주어진합니다 (Book
클래스가 name
방법을 가지고는) , 나는 이것을 할 것이다 :
NSArray *books = [self getBooks]; // etc...
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSArray *indexedBooks = [books indexUsingCollation:collation withSelector:@selector(name)];
나는이 카테고리 방법의 이름이 개선 될 수 있다는 것을 깨달았다. 그 이름이 새로운 배열보다는 수신기를 수정한다는 것을 의미한다면 더 좋은 이름은 아마도 indexedArrayUsingCollation : withSelector : 일 것입니다. 위의 카테고리에 대한 개선 사항을 환영합니다. 더 효율적 일 수 있다고 확신합니다. –