XLFormPickerCell.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // XLFormPickerCell.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "UIView+XLFormAdditions.h"
  26. #import "XLFormPickerCell.h"
  27. @interface XLFormPickerCell() <UIPickerViewDelegate, UIPickerViewDataSource>
  28. @end
  29. @implementation XLFormPickerCell
  30. @synthesize pickerView = _pickerView;
  31. @synthesize inlineRowDescriptor = _inlineRowDescriptor;
  32. -(BOOL)formDescriptorCellCanBecomeFirstResponder
  33. {
  34. return (!self.rowDescriptor.isDisabled && (self.inlineRowDescriptor == nil));
  35. }
  36. -(BOOL)formDescriptorCellBecomeFirstResponder
  37. {
  38. return [self becomeFirstResponder];
  39. }
  40. -(BOOL)canResignFirstResponder
  41. {
  42. return YES;
  43. }
  44. -(BOOL)canBecomeFirstResponder
  45. {
  46. return [self formDescriptorCellCanBecomeFirstResponder];
  47. }
  48. #pragma mark - Properties
  49. -(UIPickerView *)pickerView
  50. {
  51. if (_pickerView) return _pickerView;
  52. _pickerView = [UIPickerView autolayoutView];
  53. _pickerView.delegate = self;
  54. _pickerView.dataSource = self;
  55. return _pickerView;
  56. }
  57. #pragma mark - XLFormDescriptorCell
  58. -(void)configure
  59. {
  60. [super configure];
  61. [self.contentView addSubview:self.pickerView];
  62. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
  63. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[pickerView]-0-|" options:0 metrics:0 views:@{@"pickerView" : self.pickerView}]];
  64. }
  65. -(void)update
  66. {
  67. [super update];
  68. BOOL isDisable = self.rowDescriptor.isDisabled;
  69. self.userInteractionEnabled = !isDisable;
  70. self.contentView.alpha = isDisable ? 0.5 : 1.0;
  71. [self.pickerView selectRow:[self selectedIndex] inComponent:0 animated:NO];
  72. [self.pickerView reloadAllComponents];
  73. }
  74. +(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
  75. {
  76. return 216.0f;
  77. }
  78. #pragma mark - UIPickerViewDelegate
  79. // TWS
  80. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
  81. {
  82. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 50)];
  83. label.backgroundColor = [UIColor clearColor];
  84. label.textColor = [UIColor blackColor];
  85. label.font = [UIFont fontWithName:@"Helvetica" size:15.0];
  86. label.textAlignment = NSTextAlignmentCenter;
  87. [label setText:[self.rowDescriptor.selectorOptions objectAtIndex:row]];
  88. return label;
  89. }
  90. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  91. {
  92. if (self.inlineRowDescriptor){
  93. if (self.inlineRowDescriptor.valueTransformer){
  94. NSAssert([self.inlineRowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  95. NSValueTransformer * valueTransformer = [self.inlineRowDescriptor.valueTransformer new];
  96. NSString * tranformedValue = [valueTransformer transformedValue:[[self.inlineRowDescriptor.selectorOptions objectAtIndex:row] valueData]];
  97. if (tranformedValue){
  98. return tranformedValue;
  99. }
  100. }
  101. return [[self.inlineRowDescriptor.selectorOptions objectAtIndex:row] displayText];
  102. }
  103. if (self.rowDescriptor.valueTransformer){
  104. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  105. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  106. NSString * tranformedValue = [valueTransformer transformedValue:[[self.rowDescriptor.selectorOptions objectAtIndex:row] valueData]];
  107. if (tranformedValue){
  108. return tranformedValue;
  109. }
  110. }
  111. return [[self.rowDescriptor.selectorOptions objectAtIndex:row] displayText];
  112. }
  113. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  114. {
  115. if (self.inlineRowDescriptor){
  116. self.inlineRowDescriptor.value = [self.inlineRowDescriptor.selectorOptions objectAtIndex:row];
  117. [self.formViewController updateFormRow:self.inlineRowDescriptor];
  118. }
  119. else{
  120. [self becomeFirstResponder];
  121. self.rowDescriptor.value = [self.rowDescriptor.selectorOptions objectAtIndex:row];
  122. }
  123. }
  124. #pragma mark - UIPickerViewDataSource
  125. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  126. {
  127. return 1;
  128. }
  129. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  130. {
  131. if (self.inlineRowDescriptor){
  132. return self.inlineRowDescriptor.selectorOptions.count;
  133. }
  134. return self.rowDescriptor.selectorOptions.count;
  135. }
  136. #pragma mark - helpers
  137. -(NSInteger)selectedIndex
  138. {
  139. XLFormRowDescriptor * formRow = self.inlineRowDescriptor ?: self.rowDescriptor;
  140. if (formRow.value){
  141. for (id option in formRow.selectorOptions){
  142. if ([[option valueData] isEqual:[formRow.value valueData]]){
  143. return [formRow.selectorOptions indexOfObject:option];
  144. }
  145. }
  146. }
  147. return -1;
  148. }
  149. @end