Skip to content

Commit

Permalink
- Implemented a way to programmatically select specific index path.
Browse files Browse the repository at this point in the history
- Improved handling behavior by replacing some older approaches with newer API's.
  • Loading branch information
hackiftekhar committed Mar 17, 2018
1 parent 22ca947 commit 113a5c2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 66 deletions.
17 changes: 16 additions & 1 deletion ActionSheetPickerView/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ @interface ViewController ()<IQActionSheetPickerViewDelegate>

@implementation ViewController

- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didChangeRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == 2)
{
if (component == 0)
{
[pickerView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[pickerView selectedRowInComponent:0] inSection:1] animated:YES];
}
else if (component == 1)
{
[pickerView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[pickerView selectedRowInComponent:1] inSection:0] animated:YES];
}
}
}

-(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectTitles:(NSArray *)titles
{
switch (pickerView.tag)
Expand Down Expand Up @@ -71,7 +86,7 @@ - (IBAction)onePickerViewClicked:(UIButton *)sender
pickerView.titlesForComponents = @[@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"],@[@"AM",@"PM"]];
// pickerView.actionToolbar.tintColor = [UIColor blueAppColor];

pickerView.selectedTitles = @[@"5", @"PM"];
pickerView.selectedIndexes = @[@(4), @(1)];
[pickerView show];


Expand Down
4 changes: 2 additions & 2 deletions IQActionSheetPickerView.podspec.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "IQActionSheetPickerView",
"version": "1.0.8",
"version": "2.0.0",
"source": {
"git": "https://github.com/hackiftekhar/IQActionSheetPickerView.git",
"tag": "v1.0.8"
"tag": "v2.0.0"
},
"summary": "ActionSheet with UIPickerView",
"homepage": "http://github.com/hackiftekhar/IQActionSheetPickerView",
Expand Down
34 changes: 24 additions & 10 deletions IQActionSheetPickerView/IQActionSheetPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ typedef NS_ENUM(NSUInteger, IQActionSheetPickerStyle) {
@protocol IQActionSheetPickerViewDelegate <NSObject>

@optional
- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectTitles:(nonnull NSArray<NSString*>*)titles;
- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectTitlesAtIndexes:(nonnull NSArray<NSNumber*>*)indexes;
- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectTitles:(nonnull NSArray<NSString*>*)titles __attribute__((deprecated("This is replaced by `actionSheetPickerView:didSelectTitlesAtIndexes`."))); //If you implemented `actionSheetPickerView:didSelectTitlesAtIndexes:` delegate method then this method will not get called.


- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectDate:(nonnull NSDate*)date;


- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didChangeRow:(NSInteger)row inComponent:(NSInteger)component;


- (void)actionSheetPickerViewDidCancel:(nonnull IQActionSheetPickerView *)pickerView;
- (void)actionSheetPickerViewWillCancel:(nonnull IQActionSheetPickerView *)pickerView;
@end
Expand Down Expand Up @@ -126,14 +133,26 @@ typedef NS_ENUM(NSUInteger, IQActionSheetPickerStyle) {
///-----------------------------------------

/*!
selected titles for each component. (Not Animated)
selected indexes for each component. (Not Animated)
*/
@property(nullable, nonatomic, strong) NSArray<NSNumber*> *selectedIndexes;

/*!
Select the provided index row for each component. Ignore if actionSheetPickerStyle is IQActionSheetPickerStyleDatePicker.
*/
-(void)setSelectedIndexes:(nonnull NSArray<NSNumber*> *)selectedIndexes animated:(BOOL)animated;


/*!
get selected row in component.
*/
@property(nullable, nonatomic, strong) NSArray<NSString*> *selectedTitles;
-(NSInteger)selectedRowInComponent:(NSUInteger)component;

/*!
set selected titles for each component.
Select a row in pickerView.
*/
-(void)setSelectedTitles:(nonnull NSArray<NSString*> *)selectedTitles animated:(BOOL)animated;
-(void)selectRowAtIndexPath:(nonnull NSIndexPath*)indexPath;
-(void)selectRowAtIndexPath:(nonnull NSIndexPath*)indexPath animated:(BOOL)animated;

/*!
Titles to show for component. For example. @[ @[ @"1", @"2", @"3", ], @[ @"11", @"12", @"13", ], @[ @"21", @"22", @"23", ]].
Expand All @@ -158,11 +177,6 @@ typedef NS_ENUM(NSUInteger, IQActionSheetPickerStyle) {
*/
@property(nullable, nonatomic, strong) UIColor *pickerComponentsColor UI_APPEARANCE_SELECTOR;

/*!
Select the provided index row for each component. Ignore if actionSheetPickerStyle is IQActionSheetPickerStyleDatePicker.
*/
-(void)selectIndexes:(nonnull NSArray<NSNumber*> *)indexes animated:(BOOL)animated;

/*!
If YES then it will force to scroll third picker component to pick equal or larger row then the first.
*/
Expand Down
123 changes: 70 additions & 53 deletions IQActionSheetPickerView/IQActionSheetPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,29 +182,47 @@ -(void)pickerDoneClicked:(UIBarButtonItem*)barButton
{
case IQActionSheetPickerStyleTextPicker:
{
NSMutableArray *selectedTitles = [[NSMutableArray alloc] init];
NSMutableArray<NSNumber*> *selectedIndexes = [[NSMutableArray alloc] init];

for (NSInteger component = 0; component<_pickerView.numberOfComponents; component++)
{
NSInteger row = [_pickerView selectedRowInComponent:component];

if (row!= -1)
{
[selectedTitles addObject:_titlesForComponents[component][row]];
}
else
{
[selectedTitles addObject:[NSNull null]];
}
[selectedIndexes addObject:@(row)];
}

[self setSelectedTitles:selectedTitles];
[self setSelectedIndexes:selectedIndexes];

if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitles:)])
if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitlesAtIndexes:)])
{
[self.delegate actionSheetPickerView:self didSelectTitles:selectedTitles];
[self.delegate actionSheetPickerView:self didSelectTitlesAtIndexes:selectedIndexes];
}
else
{
NSMutableArray<NSString*> *selectedTitles = [[NSMutableArray alloc] init];

for (NSUInteger component = 0; component<selectedIndexes.count; component++)
{
NSInteger row = [selectedIndexes[component] integerValue];

if (row != -1)
{
[selectedTitles addObject:_titlesForComponents[component][row]];
}
else
{
[selectedTitles addObject:@""];
}
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitles:)])
{
[self.delegate actionSheetPickerView:self didSelectTitles:selectedTitles];
}
#pragma clang diagnostic pop
}

}
break;
case IQActionSheetPickerStyleDatePicker:
Expand Down Expand Up @@ -270,85 +288,84 @@ -(void)reloadAllComponents
[_pickerView reloadAllComponents];
}

-(void)setSelectedTitles:(NSArray *)selectedTitles
-(void)setSelectedIndexes:(NSArray<NSNumber *> *)selectedIndexes
{
[self setSelectedTitles:selectedTitles animated:NO];
[self setSelectedIndexes:selectedIndexes animated:NO];
}

-(NSArray *)selectedTitles
-(NSArray<NSNumber *> *)selectedIndexes
{
if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
{
NSMutableArray *selectedTitles = [[NSMutableArray alloc] init];

NSUInteger totalComponent = _pickerView.numberOfComponents;
NSMutableArray<NSNumber*> *selectedIndexes = [[NSMutableArray alloc] init];

for (NSInteger component = 0; component<totalComponent; component++)
for (NSInteger component = 0; component<_pickerView.numberOfComponents; component++)
{
NSInteger selectedRow = [_pickerView selectedRowInComponent:component];
NSInteger row = [_pickerView selectedRowInComponent:component];

if (selectedRow == -1)
{
[selectedTitles addObject:[NSNull null]];
}
else
{
NSArray *items = _titlesForComponents[component];

if ([items count] > selectedRow)
{
id selectTitle = items[selectedRow];
[selectedTitles addObject:selectTitle];
}
else
{
[selectedTitles addObject:[NSNull null]];
}
}
[selectedIndexes addObject:@(row)];
}

return selectedTitles;
return selectedIndexes;
}
else
{
return nil;
}
}

-(void)setSelectedTitles:(NSArray *)selectedTitles animated:(BOOL)animated
-(void)setSelectedIndexes:(NSArray<NSNumber *> *)selectedIndexes animated:(BOOL)animated
{
if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
{
NSUInteger totalComponent = MIN(selectedTitles.count, _pickerView.numberOfComponents);
NSUInteger totalComponent = MIN(MIN(selectedIndexes.count, _pickerView.numberOfComponents),_titlesForComponents.count);

for (NSInteger component = 0; component<totalComponent; component++)
{
NSArray *items = _titlesForComponents[component];
id selectTitle = selectedTitles[component];
NSUInteger selectIndex = [selectedIndexes[component] unsignedIntegerValue];

if ([items containsObject:selectTitle])
if (selectIndex < items.count)
{
NSUInteger rowIndex = [items indexOfObject:selectTitle];
[_pickerView selectRow:rowIndex inComponent:component animated:animated];
[_pickerView selectRow:selectIndex inComponent:component animated:animated];
}
}
}
}

-(void)selectIndexes:(NSArray *)indexes animated:(BOOL)animated
-(NSInteger)selectedRowInComponent:(NSUInteger)component
{
if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
{
NSUInteger totalComponent = MIN(indexes.count, _pickerView.numberOfComponents);
NSUInteger totalComponent = MIN(_titlesForComponents.count, _pickerView.numberOfComponents);

for (NSInteger component = 0; component<totalComponent; component++)
if (component < totalComponent)
{
NSArray *items = _titlesForComponents[component];
NSUInteger selectIndex = [indexes[component] unsignedIntegerValue];
return [_pickerView selectedRowInComponent:component];
}
}

return -1;
}

-(void)selectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self selectRowAtIndexPath:indexPath animated:NO];
}

-(void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker)
{
NSUInteger totalComponent = MIN(_titlesForComponents.count, _pickerView.numberOfComponents);

if (indexPath.section < totalComponent)
{
NSArray *items = _titlesForComponents[indexPath.section];

if (selectIndex < items.count)
if (indexPath.row < items.count)
{
[_pickerView selectRow:selectIndex inComponent:component animated:animated];
[_pickerView selectRow:indexPath.row inComponent:indexPath.section animated:animated];
}
}
}
Expand Down

0 comments on commit 113a5c2

Please sign in to comment.