XLFormInlineSelectorCell.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // XLFormInlineSelectorCell.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 "XLForm.h"
  26. #import "XLFormInlineSelectorCell.h"
  27. @interface XLFormInlineSelectorCell()
  28. @end
  29. @implementation XLFormInlineSelectorCell
  30. {
  31. UIColor * _beforeChangeColor;
  32. }
  33. - (BOOL)canBecomeFirstResponder
  34. {
  35. return YES;
  36. }
  37. -(BOOL)becomeFirstResponder
  38. {
  39. if (self.isFirstResponder){
  40. return [super becomeFirstResponder];
  41. }
  42. _beforeChangeColor = self.detailTextLabel.textColor;
  43. BOOL result = [super becomeFirstResponder];
  44. if (result){
  45. XLFormRowDescriptor * inlineRowDescriptor = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes][self.rowDescriptor.rowType]];
  46. UITableViewCell<XLFormDescriptorCell> * cell = [inlineRowDescriptor cellForFormController:self.formViewController];
  47. NSAssert([cell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)], @"inline cell must conform to XLFormInlineRowDescriptorCell");
  48. UITableViewCell<XLFormInlineRowDescriptorCell> * inlineCell = (UITableViewCell<XLFormInlineRowDescriptorCell> *)cell;
  49. inlineCell.inlineRowDescriptor = self.rowDescriptor;
  50. [self.rowDescriptor.sectionDescriptor addFormRow:inlineRowDescriptor afterRow:self.rowDescriptor];
  51. [self.formViewController ensureRowIsVisible:inlineRowDescriptor];
  52. }
  53. return result;
  54. }
  55. -(BOOL)resignFirstResponder
  56. {
  57. if (![self isFirstResponder]) {
  58. return [super resignFirstResponder];
  59. }
  60. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  61. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
  62. XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
  63. XLFormSectionDescriptor * formSection = [self.formViewController.form.formSections objectAtIndex:nextRowPath.section];
  64. BOOL result = [super resignFirstResponder];
  65. if (result) {
  66. [formSection removeFormRow:nextFormRow];
  67. }
  68. return result;
  69. }
  70. #pragma mark - XLFormDescriptorCell
  71. -(void)configure
  72. {
  73. [super configure];
  74. }
  75. -(void)update
  76. {
  77. [super update];
  78. self.accessoryType = UITableViewCellAccessoryNone;
  79. self.editingAccessoryType = UITableViewCellAccessoryNone;
  80. [self.textLabel setText:self.rowDescriptor.title];
  81. self.selectionStyle = self.rowDescriptor.isDisabled ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
  82. self.textLabel.text = [NSString stringWithFormat:@"%@%@", self.rowDescriptor.title, self.rowDescriptor.required && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle ? @"*" : @""];
  83. self.detailTextLabel.text = [self valueDisplayText];
  84. }
  85. -(BOOL)formDescriptorCellCanBecomeFirstResponder
  86. {
  87. return !(self.rowDescriptor.isDisabled);
  88. }
  89. -(BOOL)formDescriptorCellBecomeFirstResponder
  90. {
  91. if ([self isFirstResponder]){
  92. [self resignFirstResponder];
  93. return NO;
  94. }
  95. return [self becomeFirstResponder];
  96. }
  97. -(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  98. {
  99. [controller.tableView deselectRowAtIndexPath:[controller.form indexPathOfFormRow:self.rowDescriptor] animated:YES];
  100. }
  101. -(void)highlight
  102. {
  103. [super highlight];
  104. self.detailTextLabel.textColor = self.tintColor;
  105. }
  106. -(void)unhighlight
  107. {
  108. [super unhighlight];
  109. self.detailTextLabel.textColor = _beforeChangeColor;
  110. }
  111. #pragma mark - Helpers
  112. -(NSString *)valueDisplayText
  113. {
  114. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelector] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelectorPopover]){
  115. if (!self.rowDescriptor.value || [self.rowDescriptor.value count] == 0){
  116. return self.rowDescriptor.noValueDisplayText;
  117. }
  118. if (self.rowDescriptor.valueTransformer){
  119. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  120. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  121. NSString * tranformedValue = [valueTransformer transformedValue:self.rowDescriptor.value];
  122. if (tranformedValue){
  123. return tranformedValue;
  124. }
  125. }
  126. NSMutableArray * descriptionArray = [NSMutableArray arrayWithCapacity:[self.rowDescriptor.value count]];
  127. for (id option in self.rowDescriptor.selectorOptions) {
  128. NSArray * selectedValues = self.rowDescriptor.value;
  129. if ([selectedValues formIndexForItem:option] != NSNotFound){
  130. if (self.rowDescriptor.valueTransformer){
  131. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  132. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  133. NSString * tranformedValue = [valueTransformer transformedValue:option];
  134. if (tranformedValue){
  135. [descriptionArray addObject:tranformedValue];
  136. }
  137. }
  138. else{
  139. [descriptionArray addObject:[option displayText]];
  140. }
  141. }
  142. }
  143. return [descriptionArray componentsJoinedByString:@", "];
  144. }
  145. if (!self.rowDescriptor.value){
  146. return self.rowDescriptor.noValueDisplayText;
  147. }
  148. if (self.rowDescriptor.valueTransformer){
  149. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  150. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  151. NSString * tranformedValue = [valueTransformer transformedValue:self.rowDescriptor.value];
  152. if (tranformedValue){
  153. return tranformedValue;
  154. }
  155. }
  156. return [self.rowDescriptor.value displayText];
  157. }
  158. @end