Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Last active March 22, 2020 08:21
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lukeredpath/305676 to your computer and use it in GitHub Desktop.
Save lukeredpath/305676 to your computer and use it in GitHub Desktop.
A decorator around UILocalizedIndexedCollation that automatically adds the search icon. Updated to support ARC.
//
// LRSearchableIndexCollation.m
//
//
// Copyright (c) 2010 Luke Redpath
// Licensed under the MIT License
//
#import <UIKit/UIKit.h>
/*
A simple decorator around UILocalizedIndexedCollation that inserts the
{{search}} magnifying glass icon into the section index titles and adjusts
the section index as necessary to account for the extra index item.
Use as a direct replacement for UILocalizedIndexedCollation in indexed
table views that have a search interface.
*/
@interface LRIndexedCollationWithSearch : NSObject {
UILocalizedIndexedCollation *collation;
}
@property(nonatomic, readonly) NSArray *sectionTitles;
@property(nonatomic, readonly) NSArray *sectionIndexTitles;
+ (id)currentCollation;
- (id)initWithCollation:(UILocalizedIndexedCollation *)_collation;
- (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
- (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex;
- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;
@end
//
// FRSearchableIndexCollation.m
//
//
// Copyright (c) 2010 Luke Redpath
// Licensed under the MIT License
//
#import "LRIndexedCollationWithSearch"
@implementation LRIndexedCollationWithSearch
@dynamic sectionTitles;
@dynamic sectionIndexTitles;
+ (id)currentCollation;
{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
return [[[self alloc] initWithCollation:collation] autorelease];
}
- (id)initWithCollation:(UILocalizedIndexedCollation *)_collation;
{
if (self = [super init]) {
collation = [_collation retain];
}
return self;
}
- (void)dealloc;
{
[collation release];
[super dealloc];
}
#pragma mark -
- (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector
{
return [collation sectionForObject:object collationStringSelector:selector];
}
- (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex
{
if(indexTitleIndex == 0) {
return NSNotFound;
}
return [collation sectionForSectionIndexTitleAtIndex:indexTitleIndex-1];
}
- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector
{
return [collation sortedArrayFromArray:array collationStringSelector:selector];
}
#pragma mark -
#pragma mark Accessors
- (NSArray *)sectionTitles;
{
return [collation sectionTitles];
}
- (NSArray *)sectionIndexTitles;
{
return [[NSArray arrayWithObject:UITableViewIndexSearch] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
}
@end
Copyright (c) 2010 Luke Redpath
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
// example usage of LRIndexedCollationWithSearch within your UITableView delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[[LRIndexedCollationWithSearch currentCollation] sectionTitles] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[LRIndexedCollationWithSearch currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[LRIndexedCollationWithSearch currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[LRIndexedCollationWithSearch currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment