Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Last active March 22, 2020 08:21

Revisions

  1. lukeredpath revised this gist Aug 9, 2013. 2 changed files with 69 additions and 74 deletions.
    70 changes: 52 additions & 18 deletions LRIndexedCollationWithSearch.h
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,65 @@
    //
    // LRSearchableIndexCollation.m
    // FRSearchableIndexCollation.m
    //
    //
    // Copyright (c) 2010 Luke Redpath
    // Licensed under the MIT License
    //

    #import <UIKit/UIKit.h>
    #import "LRIndexedCollationWithSearch.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;
    @implementation LRIndexedCollationWithSearch {
    UILocalizedIndexedCollation *_collation;
    }
    @property(nonatomic, readonly) NSArray *sectionTitles;
    @property(nonatomic, readonly) NSArray *sectionIndexTitles;

    @dynamic sectionTitles;
    @dynamic sectionIndexTitles;

    + (id)currentCollation;
    - (id)initWithCollation:(UILocalizedIndexedCollation *)_collation;
    {
    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]];
    }

    - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
    - (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex;
    - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;
    @end
    73 changes: 17 additions & 56 deletions LRIndexedCollationWithSearch.m
    Original file line number Diff line number Diff line change
    @@ -1,70 +1,31 @@
    //
    // FRSearchableIndexCollation.m
    // LRSearchableIndexCollation.m
    //
    //
    // Copyright (c) 2010 Luke Redpath
    // Licensed under the MIT License
    //

    #import "LRIndexedCollationWithSearch"
    #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

    @implementation LRIndexedCollationWithSearch

    @dynamic sectionTitles;
    @dynamic sectionIndexTitles;
    @property(nonatomic, readonly) NSArray *sectionTitles;
    @property(nonatomic, readonly) NSArray *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];
    }
    - (id)initWithCollation:(UILocalizedIndexedCollation *)collation;

    - (NSArray *)sectionIndexTitles;
    {
    return [[NSArray arrayWithObject:UITableViewIndexSearch] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
    }
    - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
    - (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex;
    - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;

    @end
  2. lukeredpath revised this gist Sep 2, 2010. 2 changed files with 0 additions and 0 deletions.
  3. lukeredpath revised this gist Sep 2, 2010. 3 changed files with 9 additions and 9 deletions.
    4 changes: 2 additions & 2 deletions FRSearchableIndexCollation.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // FRSearchableIndexCollation.m
    // LRSearchableIndexCollation.m
    //
    //
    // Copyright (c) 2010 Luke Redpath
    @@ -16,7 +16,7 @@
    Use as a direct replacement for UILocalizedIndexedCollation in indexed
    table views that have a search interface.
    */
    @interface FRIndexedCollationWithSearch : NSObject {
    @interface LRIndexedCollationWithSearch : NSObject {
    UILocalizedIndexedCollation *collation;
    }
    @property(nonatomic, readonly) NSArray *sectionTitles;
    4 changes: 2 additions & 2 deletions FRSearchableIndexCollation.m
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@
    // Licensed under the MIT License
    //

    #import "FRIndexedCollationWithSearch.h"
    #import "LRIndexedCollationWithSearch"


    @implementation FRIndexedCollationWithSearch
    @implementation LRIndexedCollationWithSearch

    @dynamic sectionTitles;
    @dynamic sectionIndexTitles;
    10 changes: 5 additions & 5 deletions MyTableViewController.m
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,20 @@
    // example usage of FRIndexedCollationWithSearch within your UITableView delegate methods
    // example usage of LRIndexedCollationWithSearch within your UITableView delegate methods

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[[FRIndexedCollationWithSearch currentCollation] sectionTitles] count];
    return [[[LRIndexedCollationWithSearch currentCollation] sectionTitles] count];
    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    return [[[FRIndexedCollationWithSearch currentCollation] sectionTitles] objectAtIndex:section];
    return [[[LRIndexedCollationWithSearch currentCollation] sectionTitles] objectAtIndex:section];
    }

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
    return [[FRIndexedCollationWithSearch currentCollation] sectionIndexTitles];
    return [[LRIndexedCollationWithSearch currentCollation] sectionIndexTitles];
    }

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
    return [[FRIndexedCollationWithSearch currentCollation] sectionForSectionIndexTitleAtIndex:index];
    return [[LRIndexedCollationWithSearch currentCollation] sectionForSectionIndexTitleAtIndex:index];
    }
  4. lukeredpath revised this gist Feb 19, 2010. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions MyTableViewController.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // example usage of FRIndexedCollationWithSearch within your UITableView delegate methods

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[[FRIndexedCollationWithSearch currentCollation] sectionTitles] count];
    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    return [[[FRIndexedCollationWithSearch currentCollation] sectionTitles] objectAtIndex:section];
    }

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
    return [[FRIndexedCollationWithSearch currentCollation] sectionIndexTitles];
    }

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
    return [[FRIndexedCollationWithSearch currentCollation] sectionForSectionIndexTitleAtIndex:index];
    }
  5. lukeredpath revised this gist Feb 16, 2010. 3 changed files with 25 additions and 6 deletions.
    6 changes: 3 additions & 3 deletions FRSearchableIndexCollation.h
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    //
    // LJSearchableIndexCollation.h
    // FRSearchableIndexCollation.m
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    // Copyright 2010 LJR Software Limited. All rights reserved.
    // Copyright (c) 2010 Luke Redpath
    // Licensed under the MIT License
    //

    #import <UIKit/UIKit.h>
    6 changes: 3 additions & 3 deletions FRSearchableIndexCollation.m
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    //
    // LJSearchableIndexCollation.m
    // FRSearchableIndexCollation.m
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    // Copyright 2010 LJR Software Limited. All rights reserved.
    // Copyright (c) 2010 Luke Redpath
    // Licensed under the MIT License
    //

    #import "FRIndexedCollationWithSearch.h"
    19 changes: 19 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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.
  6. lukeredpath revised this gist Feb 16, 2010. 2 changed files with 6 additions and 6 deletions.
    8 changes: 4 additions & 4 deletions FRSearchableIndexCollation.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // FRSearchableIndexCollation.h
    // LJSearchableIndexCollation.h
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    @@ -25,7 +25,7 @@
    + (id)currentCollation;
    - (id)initWithCollation:(UILocalizedIndexedCollation *)_collation;

    - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector
    - (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex
    - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector
    - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
    - (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex;
    - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;
    @end
    4 changes: 2 additions & 2 deletions FRSearchableIndexCollation.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // FRSearchableIndexCollation.m
    // LJSearchableIndexCollation.m
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    @@ -51,7 +51,7 @@ - (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex

    - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector
    {
    [collation sortedArrayFromArray:array collationStringSelector:selector];
    return [collation sortedArrayFromArray:array collationStringSelector:selector];
    }

    #pragma mark -
  7. lukeredpath revised this gist Feb 16, 2010. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion FRSearchableIndexCollation.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // LJSearchableIndexCollation.h
    // FRSearchableIndexCollation.h
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    2 changes: 1 addition & 1 deletion FRSearchableIndexCollation.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // LJSearchableIndexCollation.m
    // FRSearchableIndexCollation.m
    //
    //
    // Created by Luke Redpath on 16/02/2010.
  8. lukeredpath revised this gist Feb 16, 2010. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions FRSearchableIndexCollation.h
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    // Copyright 2010 LJR Software Limited. All rights reserved.
    //

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>

    /*
    A simple decorator around UILocalizedIndexedCollation that inserts the
    @@ -16,7 +16,7 @@
    Use as a direct replacement for UILocalizedIndexedCollation in indexed
    table views that have a search interface.
    */
    @interface FRSearchableIndexCollation : NSObject {
    @interface FRIndexedCollationWithSearch : NSObject {
    UILocalizedIndexedCollation *collation;
    }
    @property(nonatomic, readonly) NSArray *sectionTitles;
    4 changes: 2 additions & 2 deletions FRSearchableIndexCollation.m
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@
    // Copyright 2010 LJR Software Limited. All rights reserved.
    //

    #import "FRSearchableIndexCollation.h"
    #import "FRIndexedCollationWithSearch.h"


    @implementation FRSearchableIndexCollation
    @implementation FRIndexedCollationWithSearch

    @dynamic sectionTitles;
    @dynamic sectionIndexTitles;
  9. lukeredpath created this gist Feb 16, 2010.
    31 changes: 31 additions & 0 deletions FRSearchableIndexCollation.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    //
    // LJSearchableIndexCollation.h
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    // Copyright 2010 LJR Software Limited. All rights reserved.
    //

    #import <Foundation/Foundation.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 FRSearchableIndexCollation : 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
    70 changes: 70 additions & 0 deletions FRSearchableIndexCollation.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    //
    // LJSearchableIndexCollation.m
    //
    //
    // Created by Luke Redpath on 16/02/2010.
    // Copyright 2010 LJR Software Limited. All rights reserved.
    //

    #import "FRSearchableIndexCollation.h"


    @implementation FRSearchableIndexCollation

    @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
    {
    [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