From 1dd6580af38a809b6fb32a228bb96dcab22e3410 Mon Sep 17 00:00:00 2001 From: MarK Date: Mon, 19 Nov 2012 19:04:14 +0200 Subject: [PATCH 1/2] readOnly rows implemented. Now there can be some values that cannot be checked/unchecked. Their initial selection state will remain read-only. --- Classes/ALPickerView.h | 2 + Classes/ALPickerView.m | 20 ++++++++- Classes/ALPickerViewCell.h | 4 +- Classes/ALPickerViewCell.m | 67 +++++++++++++++++++++++++--- Demo/Classes/DemoViewController.h | 1 + Demo/Classes/DemoViewController.m | 31 ++++++++++--- Demo/Demo.xcodeproj/project.pbxproj | 4 ++ ImageRes/check_disabled@2x.png | Bin 0 -> 941 bytes 8 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 ImageRes/check_disabled@2x.png diff --git a/Classes/ALPickerView.h b/Classes/ALPickerView.h index 0498f8f..6a76662 100644 --- a/Classes/ALPickerView.h +++ b/Classes/ALPickerView.h @@ -60,6 +60,8 @@ - (NSString *)pickerView:(ALPickerView *)pickerView textForRow:(NSInteger)row; // Return a boolean selection state on the given row - (BOOL)pickerView:(ALPickerView *)pickerView selectionStateForRow:(NSInteger)row; +// Return a boolean enabled state on the given row +- (BOOL)pickerView:(ALPickerView *)pickerView readOnlyStateForRow:(NSInteger)row; @optional diff --git a/Classes/ALPickerView.m b/Classes/ALPickerView.m index e006281..1849f3d 100644 --- a/Classes/ALPickerView.m +++ b/Classes/ALPickerView.m @@ -139,13 +139,18 @@ - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:( } } cell.selectionState = allSelected; + cell.readOnlyState = NO; } else { int actualRow = indexPath.row - (allOptionTitle ? 3 : 2); cell.textLabel.text = [delegate_ pickerView:self textForRow:actualRow]; cell.selectionState = [delegate_ pickerView:self selectionStateForRow:actualRow]; + cell.readOnlyState = [delegate_ pickerView:self readOnlyStateForRow:actualRow]; } - cell.selectionStyle = UITableViewCellSelectionStyleBlue; + if (cell.readOnlyState) + cell.selectionStyle = UITableViewCellSelectionStyleNone; + else + cell.selectionStyle = UITableViewCellSelectionStyleBlue; } return cell; @@ -200,6 +205,19 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath } +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { + ALPickerViewCell *cell = (ALPickerViewCell *)[tableView cellForRowAtIndexPath:indexPath]; + int actualRow = indexPath.row - (allOptionTitle ? 3 : 2); + if (!cell.readOnlyState || (allOptionTitle && actualRow == -1)) { + return indexPath; + } + else { + return nil; + } +} + + #pragma mark - ScrollView //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/Classes/ALPickerViewCell.h b/Classes/ALPickerViewCell.h index 1ca8bff..b052da5 100644 --- a/Classes/ALPickerViewCell.h +++ b/Classes/ALPickerViewCell.h @@ -31,10 +31,12 @@ @interface ALPickerViewCell : UITableViewCell { @private - BOOL selectionState_; + BOOL selectionState_; + BOOL readOnlyState_; } @property (nonatomic, assign) BOOL selectionState; +@property (nonatomic, assign) BOOL readOnlyState; - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier; diff --git a/Classes/ALPickerViewCell.m b/Classes/ALPickerViewCell.m index 08df191..3bb9340 100644 --- a/Classes/ALPickerViewCell.m +++ b/Classes/ALPickerViewCell.m @@ -35,6 +35,7 @@ @implementation ALPickerViewCell - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier { if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) { selectionState_ = NO; + readOnlyState_ = NO; self.textLabel.font = [UIFont boldSystemFontOfSize:21]; } return self; @@ -60,15 +61,28 @@ - (BOOL)selectionState { - (void)setSelectionState:(BOOL)selectionState { selectionState_ = selectionState; - if (selectionState_ != NO) { - self.imageView.image = [UIImage imageNamed:@"check"]; - self.imageView.highlightedImage = [UIImage imageNamed:@"check_selected"]; - self.textLabel.textColor = [UIColor colorWithRed:33/256. green:80/256. blue:134/256. alpha:1]; + if (readOnlyState_) { + if (selectionState_) { + self.imageView.image = [UIImage imageNamed:@"check_disabled"]; + self.imageView.highlightedImage = [UIImage imageNamed:@"check_selected"]; + } + else { + self.imageView.image = nil; + self.imageView.highlightedImage = nil; + } + self.textLabel.textColor = [UIColor colorWithRed:153/256. green:153/256. blue:153/256. alpha:1]; } else { - self.imageView.image = nil; - self.imageView.highlightedImage = nil; - self.textLabel.textColor = [UIColor blackColor]; + if (selectionState_) { + self.imageView.image = [UIImage imageNamed:@"check"]; + self.imageView.highlightedImage = [UIImage imageNamed:@"check_selected"]; + self.textLabel.textColor = [UIColor colorWithRed:33/256. green:80/256. blue:134/256. alpha:1]; + } + else { + self.imageView.image = nil; + self.imageView.highlightedImage = nil; + self.textLabel.textColor = [UIColor blackColor]; + } } [self.imageView setNeedsDisplay]; @@ -77,6 +91,45 @@ - (void)setSelectionState:(BOOL)selectionState { } +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +- (BOOL)readOnlyState { + return readOnlyState_; +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +- (void)setReadOnlyState:(BOOL)readOnlyState { + readOnlyState_ = readOnlyState; + + if (readOnlyState_) { + if (selectionState_) { + self.imageView.image = [UIImage imageNamed:@"check_disabled"]; + self.imageView.highlightedImage = [UIImage imageNamed:@"check_selected"]; + } + else { + self.imageView.image = nil; + self.imageView.highlightedImage = nil; + } + self.textLabel.textColor = [UIColor colorWithRed:153/256. green:153/256. blue:153/256. alpha:1]; + } + else { + if (selectionState_) { + self.imageView.image = [UIImage imageNamed:@"check"]; + self.imageView.highlightedImage = [UIImage imageNamed:@"check_selected"]; + self.textLabel.textColor = [UIColor colorWithRed:33/256. green:80/256. blue:134/256. alpha:1]; + } + else { + self.imageView.image = nil; + self.imageView.highlightedImage = nil; + self.textLabel.textColor = [UIColor blackColor]; + } + } + + [self.textLabel setNeedsDisplay]; + [self setNeedsLayout]; +} + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - (void)layoutSubviews { [super layoutSubviews]; diff --git a/Demo/Classes/DemoViewController.h b/Demo/Classes/DemoViewController.h index 90c5b1a..11ad319 100644 --- a/Demo/Classes/DemoViewController.h +++ b/Demo/Classes/DemoViewController.h @@ -13,6 +13,7 @@ @interface DemoViewController : UIViewController { NSArray *entries; NSMutableDictionary *selectionStates; + NSMutableDictionary *readOnlyStates; ALPickerView *pickerView; } diff --git a/Demo/Classes/DemoViewController.m b/Demo/Classes/DemoViewController.m index 60a9326..3201d8b 100644 --- a/Demo/Classes/DemoViewController.m +++ b/Demo/Classes/DemoViewController.m @@ -17,19 +17,28 @@ - (void)viewDidLoad { // Create some sample data entries = [[NSArray alloc] initWithObjects:@"Row 1", @"Row 2", @"Row 3", @"Row 4", @"Row 5", nil]; + + readOnlyStates = [[NSMutableDictionary alloc] init]; + for (NSString *key in entries) + if ([key isEqualToString:@"Row 2"] || [key isEqualToString:@"Row 4"]) + [readOnlyStates setObject:[NSNumber numberWithBool:YES] forKey:key]; + else + [readOnlyStates setObject:[NSNumber numberWithBool:NO] forKey:key]; + selectionStates = [[NSMutableDictionary alloc] init]; for (NSString *key in entries) - [selectionStates setObject:[NSNumber numberWithBool:NO] forKey:key]; + [selectionStates setObject:[NSNumber numberWithBool:YES] forKey:key]; // Init picker and add it to view pickerView = [[ALPickerView alloc] initWithFrame:CGRectMake(0, 244, 0, 0)]; pickerView.delegate = self; - [self.view addSubview:pickerView]; + [self.view addSubview:pickerView]; } - (void)dealloc { [pickerView release]; + [readOnlyStates release]; [selectionStates release]; [entries release]; [super dealloc]; @@ -51,11 +60,18 @@ - (BOOL)pickerView:(ALPickerView *)pickerView selectionStateForRow:(NSInteger)ro return [[selectionStates objectForKey:[entries objectAtIndex:row]] boolValue]; } +- (BOOL)pickerView:(ALPickerView *)pickerView readOnlyStateForRow:(NSInteger)row { + return [[readOnlyStates objectForKey:[entries objectAtIndex:row]] boolValue]; +} + - (void)pickerView:(ALPickerView *)pickerView didCheckRow:(NSInteger)row { // Check whether all rows are checked or only one if (row == -1) - for (id key in [selectionStates allKeys]) - [selectionStates setObject:[NSNumber numberWithBool:YES] forKey:key]; + for (id key in [selectionStates allKeys]) { + if (![[readOnlyStates objectForKey:key] boolValue]) { + [selectionStates setObject:[NSNumber numberWithBool:YES] forKey:key]; + } + } else [selectionStates setObject:[NSNumber numberWithBool:YES] forKey:[entries objectAtIndex:row]]; } @@ -63,8 +79,11 @@ - (void)pickerView:(ALPickerView *)pickerView didCheckRow:(NSInteger)row { - (void)pickerView:(ALPickerView *)pickerView didUncheckRow:(NSInteger)row { // Check whether all rows are unchecked or only one if (row == -1) - for (id key in [selectionStates allKeys]) - [selectionStates setObject:[NSNumber numberWithBool:NO] forKey:key]; + for (id key in [selectionStates allKeys]) { + if (![[readOnlyStates objectForKey:key] boolValue]) { + [selectionStates setObject:[NSNumber numberWithBool:NO] forKey:key]; + } + } else [selectionStates setObject:[NSNumber numberWithBool:NO] forKey:[entries objectAtIndex:row]]; } diff --git a/Demo/Demo.xcodeproj/project.pbxproj b/Demo/Demo.xcodeproj/project.pbxproj index 0080ca0..a3746f6 100755 --- a/Demo/Demo.xcodeproj/project.pbxproj +++ b/Demo/Demo.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 2899E5220DE3E06400AC0155 /* DemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2899E5210DE3E06400AC0155 /* DemoViewController.xib */; }; 28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; }; 28D7ACF80DDB3853001CB0EB /* DemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28D7ACF70DDB3853001CB0EB /* DemoViewController.m */; }; + 4D3401D1164D79D200EDD9D7 /* check_disabled@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4D3401D0164D79D200EDD9D7 /* check_disabled@2x.png */; }; FA36CA2112E4DABB00A49F9F /* ALPickerView.m in Sources */ = {isa = PBXBuildFile; fileRef = FA36CA1C12E4DABB00A49F9F /* ALPickerView.m */; }; FA8EA5EA146EC91100CCACA3 /* ALPickerViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FA8EA5E9146EC91100CCACA3 /* ALPickerViewCell.m */; }; FA8EA60A146EDEF300CCACA3 /* check@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FA8EA5EB146EC92A00CCACA3 /* check@2x.png */; }; @@ -39,6 +40,7 @@ 28D7ACF70DDB3853001CB0EB /* DemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoViewController.m; sourceTree = ""; }; 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 32CA4F630368D1EE00C91783 /* Demo_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Demo_Prefix.pch; sourceTree = ""; }; + 4D3401D0164D79D200EDD9D7 /* check_disabled@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "check_disabled@2x.png"; path = "../ImageRes/check_disabled@2x.png"; sourceTree = ""; }; 8D1107310486CEB800E47090 /* Demo-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Demo-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = ""; }; FA36CA1B12E4DABB00A49F9F /* ALPickerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ALPickerView.h; path = ../Classes/ALPickerView.h; sourceTree = SOURCE_ROOT; }; FA36CA1C12E4DABB00A49F9F /* ALPickerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ALPickerView.m; path = ../Classes/ALPickerView.m; sourceTree = SOURCE_ROOT; }; @@ -113,6 +115,7 @@ children = ( FA8EA5EB146EC92A00CCACA3 /* check@2x.png */, FA8EA5EC146EC92A00CCACA3 /* check_selected@2x.png */, + 4D3401D0164D79D200EDD9D7 /* check_disabled@2x.png */, FA8EA5ED146EC92A00CCACA3 /* frame_left@2x.png */, FA8EA5EE146EC92A00CCACA3 /* frame_middle@2x.png */, FA8EA5EF146EC92A00CCACA3 /* frame_right@2x.png */, @@ -207,6 +210,7 @@ FA8EA60E146EDEF300CCACA3 /* frame_right@2x.png in Resources */, FA8EA60F146EDEF300CCACA3 /* wheel_shadow@2x.png in Resources */, FA8EA610146EDEF300CCACA3 /* wheel_bg@2x.png in Resources */, + 4D3401D1164D79D200EDD9D7 /* check_disabled@2x.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ImageRes/check_disabled@2x.png b/ImageRes/check_disabled@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..b775f953249efa5bc95ea0df8fd1c159dc58a500 GIT binary patch literal 941 zcmV;e15*5nP)yT3OxyKmpQd%d1(UVqhU z^^x1{UN@|_>z)@-rBXTJ@pzsXU|e81E1j*q$eo$@^SCGa%Fs74ZgNOqM&H&Pa5~J~JRZ{f-R4 z`$*Lt)Xqz;BY7r3IFU~R=$O11@3-6SJnswHaiBt>umG6ht`Q6I7CY-oDwX=ibwYL= zC=dw5G5JBUfZPJ+H&QiLJHpRVpklGOgy?LE$yF_Fb3kd|=#B#U{eC(GmsB7F-3M&* zs=@ADQDd7^nkG5~RbW1qavB}pSM)Yt(2Yfw9;0?1>g+=ANXif$+Ty3gq^cG`+Ct|3 zKD=+V1vYTJ8;iwG^FgwV=#*rg)W3!3yq9#!t_MYUQEAy%ZVV!@uVGTTT&CB<5vCdw z0cf zWS?L#cpf(GT?hW(aV@V{Vd$jcaGyb0PZWDY1IL^`V| zEyHIBK!AwO5RgwwGGJaII=5xLRe$6&2v2Ai_&f-|K`eDF;=R`@G{$T%o);b++brjB%9w{)~{&;q5?>8}`Ty#4XHQRv3azxawa2t37s56-5 zhhnCWfIXYb<$ft_Q>iRdCT7LQ-%-f)f1Wi!bnuFKfmg^53b`(PoM`_8 Date: Tue, 20 Nov 2012 16:31:13 +0200 Subject: [PATCH 2/2] Read-only feature made optional. ALPickerViewDelegate method pickerView:readOnlyStateForRow: made optional. --- Classes/ALPickerView.h | 4 ++-- Classes/ALPickerView.m | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Classes/ALPickerView.h b/Classes/ALPickerView.h index 6a76662..b2a9992 100644 --- a/Classes/ALPickerView.h +++ b/Classes/ALPickerView.h @@ -60,8 +60,6 @@ - (NSString *)pickerView:(ALPickerView *)pickerView textForRow:(NSInteger)row; // Return a boolean selection state on the given row - (BOOL)pickerView:(ALPickerView *)pickerView selectionStateForRow:(NSInteger)row; -// Return a boolean enabled state on the given row -- (BOOL)pickerView:(ALPickerView *)pickerView readOnlyStateForRow:(NSInteger)row; @optional @@ -69,5 +67,7 @@ - (void)pickerView:(ALPickerView *)pickerView didCheckRow:(NSInteger)row; // Inform the delegate that a row got deselected, if row = -1 all rows are deselected - (void)pickerView:(ALPickerView *)pickerView didUncheckRow:(NSInteger)row; +// Return a boolean read-only state on the given row +- (BOOL)pickerView:(ALPickerView *)pickerView readOnlyStateForRow:(NSInteger)row; @end diff --git a/Classes/ALPickerView.m b/Classes/ALPickerView.m index 1849f3d..f11cd0b 100644 --- a/Classes/ALPickerView.m +++ b/Classes/ALPickerView.m @@ -145,7 +145,9 @@ - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:( int actualRow = indexPath.row - (allOptionTitle ? 3 : 2); cell.textLabel.text = [delegate_ pickerView:self textForRow:actualRow]; cell.selectionState = [delegate_ pickerView:self selectionStateForRow:actualRow]; - cell.readOnlyState = [delegate_ pickerView:self readOnlyStateForRow:actualRow]; + if (delegate_ && [delegate_ respondsToSelector:@selector(pickerView:readOnlyStateForRow:)]) { + cell.readOnlyState = [delegate_ pickerView:self readOnlyStateForRow:actualRow]; + } } if (cell.readOnlyState) cell.selectionStyle = UITableViewCellSelectionStyleNone;