Created
August 22, 2012 13:27
-
-
Save wannabegeek/3425537 to your computer and use it in GitHub Desktop.
Extension to a Multiple Component PickerInputTableViewCell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "TimePickerInputTableViewCell.h" | |
#import "DataFormatters.h" | |
@implementation TimePickerInputTableViewCell | |
@synthesize delegate= _delegate; | |
@synthesize time = _time; | |
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier | |
{ | |
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; | |
if (self) { | |
// Initialization code | |
self.picker.delegate = self; | |
self.picker.dataSource = self; | |
} | |
return self; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
// Initialization code | |
self.picker.delegate = self; | |
self.picker.dataSource = self; | |
} | |
return self; | |
} | |
- (void)setTime:(NSTimeInterval)value { | |
_time = value; | |
self.detailTextLabel.text = [[DataFormatters elapsedTimeFormatterHHMM] stringFromDouble:_time]; | |
double minutes = ((NSInteger)(_time/60.0)) % 60; | |
double hours = (NSInteger)(_time/3600.0); | |
[self.picker selectRow:hours inComponent:0 animated:YES]; | |
[self.picker selectRow:minutes inComponent:1 animated:YES]; | |
} | |
#pragma mark - | |
#pragma mark UIPickerViewDataSource | |
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { | |
return 2; | |
} | |
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { | |
if (component == 0) { | |
return 24; | |
} else { | |
return 60; | |
} | |
} | |
#pragma mark - | |
#pragma mark UIPickerViewDelegate | |
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { | |
return [NSString stringWithFormat:@"%02d", row]; | |
} | |
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { | |
return 44.0f; | |
} | |
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { | |
return 100.0f; //pickerView.bounds.size.width - 20.0f; | |
} | |
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { | |
self.time = ((double)[pickerView selectedRowInComponent:0] * 3600.0) + ((double)[pickerView selectedRowInComponent:1] * 60.0); | |
if (_delegate && [_delegate respondsToSelector:@selector(tableViewCell:didEndEditingWithTime:)]) { | |
[_delegate tableViewCell:self didEndEditingWithTime:_time]; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "PickerInputTableViewCell.h" | |
@class TimePickerInputTableViewCell; | |
@protocol TimePickerInputTableViewCellDelegate <NSObject> | |
@optional | |
- (void)tableViewCell:(TimePickerInputTableViewCell *)cell didEndEditingWithTime:(NSTimeInterval)time; | |
@end | |
@interface TimePickerInputTableViewCell : PickerInputTableViewCell <UIPickerViewDataSource, UIPickerViewDelegate> | |
@property (nonatomic, assign) NSTimeInterval time; | |
@property (weak) IBOutlet id <TimePickerInputTableViewCellDelegate> delegate; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment