Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Last active March 22, 2020 08:21
Show Gist options
  • 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.
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.
//
// FRSearchableIndexCollation.m
//
//
// Copyright (c) 2010 Luke Redpath
// Licensed under the MIT License
//
#import "LRIndexedCollationWithSearch.h"
@implementation LRIndexedCollationWithSearch {
UILocalizedIndexedCollation *_collation;
}
@dynamic sectionTitles;
@dynamic sectionIndexTitles;
+ (id)currentCollation;
{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
return [[self alloc] initWithCollation:collation];
}
- (id)initWithCollation:(UILocalizedIndexedCollation *)collation;
{
if (self = [super init]) {
_collation = collation;
}
return self;
}
#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
//
// 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
@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
// 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