Skip to content

Commit

Permalink
input view delegate now extends uitextfield delegate for more customi…
Browse files Browse the repository at this point in the history
…zation
  • Loading branch information
acerbetti committed May 18, 2013
1 parent 1346abd commit c5368ea
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
16 changes: 14 additions & 2 deletions ACEAutocompleteBar/ACEAutocompleteBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@

@protocol ACEAutocompleteItem <NSObject>

// used to display text on the autocomplete bar
- (NSString *)autocompleteString;

@end

#pragma mark -

@protocol ACEAutocompleteDelegate <NSObject>
@protocol ACEAutocompleteDelegate <UITextFieldDelegate>

// called when the user tap on one of the suggestions
- (void)inputView:(ACEAutocompleteInputView *)inputView didSelectObject:(id)object forField:(UITextField *)textField;

@end
Expand All @@ -43,12 +45,22 @@

@protocol ACEAutocompleteDataSource <NSObject>

// number of characters required to trigger the search on possible suggestions
- (NSUInteger)minimumCharactersToTrigger:(ACEAutocompleteInputView *)inputView;

// use the block to return the array of items asynchronously based on the query string
- (void)inputView:(ACEAutocompleteInputView *)inputView itemsFor:(NSString *)query result:(void (^)(NSArray *items))resultBlock;

@optional

// calculate the width of the view for the object (NSString or ACEAutocompleteItem)
- (CGFloat)inputView:(ACEAutocompleteInputView *)inputView widthForObject:(id)object;
- (void)inputView:(ACEAutocompleteInputView *)inputView customizeView:(UIView *)view withObject:(id)object;

// called when after the cell is created, to add custom subviews
- (void)inputView:(ACEAutocompleteInputView *)inputView customizeView:(UIView *)view;

// called to set the object properties in the custom view
- (void)inputView:(ACEAutocompleteInputView *)inputView setObject:(id)object forView:(UIView *)view;

@end

Expand Down
2 changes: 1 addition & 1 deletion ACEAutocompleteBar/ACEAutocompleteInputView.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@property (nonatomic, assign) id<ACEAutocompleteDelegate> delegate;
@property (nonatomic, assign) id<ACEAutocompleteDataSource> dataSource;

// customization
// customization (ignored when the optional methods of the data source are implemeted)
@property (nonatomic, strong) UIFont * font;
@property (nonatomic, strong) UIColor * textColor;

Expand Down
64 changes: 57 additions & 7 deletions ACEAutocompleteBar/ACEAutocompleteInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,34 @@ - (NSString *)stringForObjectAtIndex:(NSUInteger)index

#pragma mark - Text Field Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
if ([self.delegate respondsToSelector:_cmd]) {
return [self.delegate textFieldShouldBeginEditing:textField];
}
return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:_cmd]) {
[self.delegate textFieldDidBeginEditing:textField];
}
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:_cmd]) {
return [self.delegate textFieldShouldEndEditing:textField];
}
return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:_cmd]) {
[self.delegate textFieldDidEndEditing:textField];
}
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Expand All @@ -130,9 +154,30 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
self.suggestionList = nil;
[self.suggestionListView reloadData];
}

if ([self.delegate respondsToSelector:_cmd]) {
return [self.delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
}

return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if ([self.delegate respondsToSelector:_cmd]) {
return [self.delegate textFieldShouldClear:textField];
}
return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([self.delegate respondsToSelector:_cmd]) {
return [self.delegate textFieldShouldReturn:textField];
}
return NO;
}


#pragma mark - Table View Delegate

Expand Down Expand Up @@ -179,13 +224,18 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.contentView.transform = CGAffineTransformMakeRotation(M_PI/2);

// customization
cell.textLabel.font = self.font;
cell.textLabel.textColor = self.textColor;
if ([self.dataSource respondsToSelector:@selector(inputView:customizeView:)]) {
[self.dataSource inputView:self customizeView:cell.contentView];

} else {
cell.textLabel.font = self.font;
cell.textLabel.textColor = self.textColor;
}
}

// customize the cell view if the data source support it, just use the text otherwise
if ([self.dataSource respondsToSelector:@selector(inputView:customizeView:withObject:)]) {
[self.dataSource inputView:self customizeView:cell.contentView withObject:[self.suggestionList objectAtIndex:indexPath.row]];
if ([self.dataSource respondsToSelector:@selector(inputView:setObject:forView:)]) {
[self.dataSource inputView:self setObject:[self.suggestionList objectAtIndex:indexPath.row] forView:cell.contentView];

} else {
cell.textLabel.text = [self stringForObjectAtIndex:indexPath.row];
Expand Down
6 changes: 6 additions & 0 deletions ACEAutocompleteBarDemo/ACEViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ - (void)inputView:(ACEAutocompleteInputView *)inputView didSelectObject:(id)obje
textField.text = object; // nssstring
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}


#pragma mark - Autocomplete Data Source

Expand Down

0 comments on commit c5368ea

Please sign in to comment.