Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions TITokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +49,10 @@
- (CGFloat)tokenField:(TITokenField *)tokenField resultsTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
@end

@protocol TITokenFieldDataSource <NSObject>
- (NSArray *)tokenFieldSearchResults:(TITokenField *)tokenField;
@end

@interface TITokenFieldInternalDelegate : NSObject <UITextFieldDelegate>
@end

Expand All @@ -66,6 +71,7 @@
@property (weak, nonatomic, readonly) NSArray * tokenTitles;

- (void)updateContentSize;
- (void)reloadSearchResults;

@end

Expand All @@ -79,6 +85,7 @@ typedef enum {

@interface TITokenField : UITextField
@property (nonatomic, weak) id <TITokenFieldDelegate> delegate;
@property (nonatomic, weak) id <TITokenFieldDataSource> dataSource;
@property (weak, nonatomic, readonly) NSArray * tokens;
@property (weak, nonatomic, readonly) TIToken * selectedToken;
@property (weak, nonatomic, readonly) NSArray * tokenTitles;
Expand Down
33 changes: 27 additions & 6 deletions TITokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}
Expand Down Expand Up @@ -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){

Expand Down Expand Up @@ -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];
Expand Down