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

readOnly rows implemented. #4

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
2 changes: 2 additions & 0 deletions Classes/ALPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,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
22 changes: 21 additions & 1 deletion Classes/ALPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,20 @@ - (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];
if (delegate_ && [delegate_ respondsToSelector:@selector(pickerView:readOnlyStateForRow:)]) {
cell.readOnlyState = [delegate_ pickerView:self readOnlyStateForRow:actualRow];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
if (cell.readOnlyState)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
else
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}

return cell;
Expand Down Expand Up @@ -200,6 +207,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

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 3 additions & 1 deletion Classes/ALPickerViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
67 changes: 60 additions & 7 deletions Classes/ALPickerViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions Demo/Classes/DemoViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@interface DemoViewController : UIViewController <ALPickerViewDelegate> {
NSArray *entries;
NSMutableDictionary *selectionStates;
NSMutableDictionary *readOnlyStates;

ALPickerView *pickerView;
}
Expand Down
31 changes: 25 additions & 6 deletions Demo/Classes/DemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -51,20 +60,30 @@ - (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]];
}

- (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]];
}
Expand Down
4 changes: 4 additions & 0 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -39,6 +40,7 @@
28D7ACF70DDB3853001CB0EB /* DemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoViewController.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* Demo_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Demo_Prefix.pch; sourceTree = "<group>"; };
4D3401D0164D79D200EDD9D7 /* check_disabled@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "check_disabled@2x.png"; path = "../ImageRes/check_disabled@2x.png"; sourceTree = "<group>"; };
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 = "<group>"; };
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; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
};
Expand Down
Binary file added ImageRes/check_disabled@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.