Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for dynamic data binding using datasource and delegate #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
28 changes: 25 additions & 3 deletions Pod/Classes/DownPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

#import <UIKit/UIKit.h>

@protocol DownPickerDataSource;
@protocol DownPickerDelegate;

@interface DownPicker : UIControl<UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
{
UIPickerView* pickerView;
IBOutlet UITextField* textField;
NSArray* dataArray;
NSMutableArray* dataArray;
NSString* placeholder;
NSString* placeholderWhileSelecting;
NSString* toolbarDoneButtonText;
Expand All @@ -25,6 +28,11 @@

@property (nonatomic) NSString* text;
@property (nonatomic) NSInteger selectedIndex;
@property (strong, nonatomic) id<DownPickerDelegate> delegate;
@property (strong, nonatomic) id<DownPickerDataSource> dataSource;

- (instancetype) initWithTextField: (UITextField *) uiTextField AndDataSource: (id<DownPickerDataSource>) dataSource;
- (void) reloadData;

-(id)initWithTextField:(UITextField *)tf;
-(id)initWithTextField:(UITextField *)tf withData:(NSArray*) data;
Expand Down Expand Up @@ -65,8 +73,22 @@

/**
Sets the zero-based index of the selected item: -1 can be used to clear selection.
@return
The value at the given index or NIL if nothing has been selected yet.
*/
-(void) setValueAtIndex:(NSInteger)index;
@end

@protocol DownPickerDataSource <NSObject>

- (NSInteger) numberOfItemsForDownPicker: (DownPicker *) picker;
- (NSString *) downPicker: (DownPicker *) picker textForIndex: (NSInteger) index;

@end

@protocol DownPickerDelegate <NSObject>

@optional
- (void) downPicker: (DownPicker *) picker didSelectItemAtIndex: (NSInteger) index;
- (void) downPicker: (DownPicker *) picker didPickedItemAtIndex: (NSInteger) index;
- (void) downPickerDidCancelSelection: (DownPicker *) picker;

@end
80 changes: 73 additions & 7 deletions Pod/Classes/DownPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,52 @@ -(id)initWithTextField:(UITextField *)tf withData:(NSArray*) data
return self;
}

- (instancetype) initWithTextField: (UITextField *) uiTextField AndDataSource: (id<DownPickerDataSource>) dataSource
{
self = [self initWithTextField:uiTextField];
if (self)
{
self.dataSource = dataSource;
if ([self.dataSource respondsToSelector:@selector(numberOfItemsForDownPicker:)] && [self.dataSource respondsToSelector:@selector(downPicker:textForIndex:)])
{
NSInteger numberOfItems = [self.dataSource numberOfItemsForDownPicker:self];
if (!dataArray)
dataArray = [[NSMutableArray alloc] init];
[dataArray removeAllObjects];
for (int i = 0; i < (int) numberOfItems; i++)
{
[dataArray addObject:[self.dataSource downPicker:self textForIndex:i]];
}
}
else
{
NSException *invalidArgumentException = [NSException exceptionWithName:NSInvalidArgumentException reason:@"Data Source must implement numberOfItems and textForIndex methods." userInfo:nil];
[invalidArgumentException raise];
}
}
return self;
}

- (void) reloadData
{
if ([self.dataSource respondsToSelector:@selector(numberOfItemsForDownPicker:)] && [self.dataSource respondsToSelector:@selector(downPicker:textForIndex:)])
{
NSInteger numberOfItems = [self.dataSource numberOfItemsForDownPicker:self];
if (!dataArray)
dataArray = [[NSMutableArray alloc] init];
[dataArray removeAllObjects];
for (int i = 0; i < (int) numberOfItems; i++)
{
[dataArray addObject:[self.dataSource downPicker:self textForIndex:i]];
}
}
else
{
NSException *invalidArgumentException = [NSException exceptionWithName:NSInvalidArgumentException reason:@"Data Source must implement numberOfItems and textForIndex methods." userInfo:nil];
[invalidArgumentException raise];
}
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
Expand All @@ -82,6 +128,11 @@ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComp
{
self->textField.text = [dataArray objectAtIndex:row];
[self sendActionsForControlEvents:UIControlEventValueChanged];

if ([self.delegate respondsToSelector:@selector(downPicker:didSelectItemAtIndex:)])
{
[self.delegate downPicker:self didSelectItemAtIndex:row];
}
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
Expand Down Expand Up @@ -112,6 +163,11 @@ -(void)doneClicked:(id) sender
}
*/
[self sendActionsForControlEvents:UIControlEventValueChanged];

if ([self.delegate respondsToSelector:@selector(downPicker:didPickedItemAtIndex:)])
{
[self.delegate downPicker:self didPickedItemAtIndex:[self selectedIndex]];
}
}

-(void)cancelClicked:(id)sender
Expand All @@ -121,6 +177,11 @@ -(void)cancelClicked:(id)sender
self->textField.placeholder = self->placeholder;
}
self->textField.text = _previousSelectedString;

if ([self.delegate respondsToSelector:@selector(downPickerDidCancelSelection:)])
{
[self.delegate downPickerDidCancelSelection:self];
}
}


Expand Down Expand Up @@ -197,7 +258,8 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField
[self sendActionsForControlEvents:UIControlEventEditingDidBegin];
}

- (void)textFieldDidEndEditing:(UITextField *)aTextField {
- (void) textFieldDidEndEditing: (UITextField *) aTextField
{
// [self doneClicked:aTextField];
aTextField.userInteractionEnabled = YES;
[self sendActionsForControlEvents:UIControlEventEditingDidEnd];
Expand All @@ -208,18 +270,19 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
return NO;
}

-(void) setData:(NSArray*) data
-(void) setData: (NSMutableArray *) data
{
dataArray = data;
}

-(void) showArrowImage:(BOOL)b
-(void) showArrowImage: (BOOL) showArrow
{
if (b == YES) {
// set the DownPicker arrow to the right (you can replace it with any 32x24 px transparent image: changing size might give different results)
if (showArrow == YES)
{
self->textField.rightViewMode = UITextFieldViewModeAlways;
}
else {
else
{
self->textField.rightViewMode = UITextFieldViewModeNever;
}
}
Expand Down Expand Up @@ -277,7 +340,10 @@ -(NSString*) getValueAtIndex:(NSInteger)index

-(void) setValueAtIndex:(NSInteger)index
{
if (index >= 0) [self pickerView:nil didSelectRow:index inComponent:0];
if (pickerView && index >= 0)
{
[self pickerView:pickerView didSelectRow:index inComponent:0];
}
else [self setText:nil];
}

Expand Down