From 956efe307f2f376b815331fd5ab675d81a75d827 Mon Sep 17 00:00:00 2001 From: AbhaySingh Date: Thu, 5 Dec 2013 17:15:19 +0530 Subject: [PATCH] Use data source for getting search results. Pick search results from data source if it is set and call 'reloadSearchResults'. This gives flexibility to write custom logic for getting search results. --- TITokenField.h | 7 +++++++ TITokenField.m | 33 +++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/TITokenField.h b/TITokenField.h index 6602c5a..c2f05a4 100644 --- a/TITokenField.h +++ b/TITokenField.h @@ -40,6 +40,7 @@ - (BOOL)tokenField:(TITokenField *)tokenField willRemoveToken:(TIToken *)token; - (void)tokenField:(TITokenField *)tokenField didRemoveToken:(TIToken *)token; +- (void)tokenField:(TITokenField *)tokenField didChangeText:(NSString *)text; - (void)tokenField:(TITokenField *)tokenField didFinishSearch:(NSArray *)matches; - (NSString *)tokenField:(TITokenField *)tokenField displayStringForRepresentedObject:(id)object; - (NSString *)tokenField:(TITokenField *)tokenField searchResultStringForRepresentedObject:(id)object; @@ -48,6 +49,10 @@ - (CGFloat)tokenField:(TITokenField *)tokenField resultsTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; @end +@protocol TITokenFieldDataSource +- (NSArray *)tokenFieldSearchResults:(TITokenField *)tokenField; +@end + @interface TITokenFieldInternalDelegate : NSObject @end @@ -66,6 +71,7 @@ @property (weak, nonatomic, readonly) NSArray * tokenTitles; - (void)updateContentSize; +- (void)reloadSearchResults; @end @@ -79,6 +85,7 @@ typedef enum { @interface TITokenField : UITextField @property (nonatomic, weak) id delegate; +@property (nonatomic, weak) id dataSource; @property (weak, nonatomic, readonly) NSArray * tokens; @property (weak, nonatomic, readonly) TIToken * selectedToken; @property (weak, nonatomic, readonly) NSArray * tokenTitles; diff --git a/TITokenField.m b/TITokenField.m index ad8b8d3..a6f7462 100644 --- a/TITokenField.m +++ b/TITokenField.m @@ -243,8 +243,16 @@ - (void)tokenFieldDidEndEditing:(TITokenField *)field { } - (void)tokenFieldTextDidChange:(TITokenField *)field { - [self resultsForSearchString:_tokenField.text]; - + if ([_tokenField.delegate respondsToSelector:@selector(tokenField:didChangeText:)]) { + [_tokenField.delegate tokenField:_tokenField didChangeText:_tokenField.text]; + } + + if (_tokenField.dataSource != nil) { + [self reloadSearchResults]; + return; + } + + [self resultsForSearchString:_tokenField.text]; if (_forcePickSearchResult) [self setSearchResultsVisible:YES]; else [self setSearchResultsVisible:(_resultsArray.count > 0)]; } @@ -313,12 +321,12 @@ - (void)resultsForSearchString:(NSString *)searchString { // If the source is massive, this could take some time. // You could always subclass and override this if needed or do it on a background thread. // GCD would be great for that. - + + searchString = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [_resultsArray removeAllObjects]; [_resultsTable reloadData]; - - searchString = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - + if (searchString.length || _forcePickSearchResult){ [_sourceArray enumerateObjectsUsingBlock:^(id sourceObject, NSUInteger idx, BOOL *stop){ @@ -354,6 +362,19 @@ - (void)resultsForSearchString:(NSString *)searchString { } } +- (void)reloadSearchResults { + if (_tokenField.dataSource != nil) { + NSArray *searchResults = [_tokenField.dataSource tokenFieldSearchResults:_tokenField]; + _resultsArray = [NSMutableArray arrayWithArray:searchResults]; + } + if (_forcePickSearchResult) { + [self setSearchResultsVisible:YES]; + } else { + [self setSearchResultsVisible:(_resultsArray.count > 0)]; + } + [_resultsTable reloadData]; +} + - (void)presentpopoverAtTokenFieldCaretAnimated:(BOOL)animated { UITextPosition * position = [_tokenField positionFromPosition:_tokenField.beginningOfDocument offset:2];