XLFormSegmentedCell.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // XLFormSegmentedCell.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 "XLFormSegmentedCell.h"
  26. #import "NSObject+XLFormAdditions.h"
  27. #import "UIView+XLFormAdditions.h"
  28. @interface XLFormSegmentedCell()
  29. @property NSMutableArray * dynamicCustomConstraints;
  30. @end
  31. @implementation XLFormSegmentedCell
  32. @synthesize textLabel = _textLabel;
  33. @synthesize segmentedControl = _segmentedControl;
  34. #pragma mark - XLFormDescriptorCell
  35. -(void)configure
  36. {
  37. [super configure];
  38. self.selectionStyle = UITableViewCellSelectionStyleNone;
  39. [self.contentView addSubview:self.segmentedControl];
  40. [self.contentView addSubview:self.textLabel];
  41. [self.textLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:0];
  42. [self.segmentedControl addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
  43. }
  44. -(void)update
  45. {
  46. [super update];
  47. self.textLabel.text = [NSString stringWithFormat:@"%@%@", self.rowDescriptor.title, self.rowDescriptor.required && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle ? @"*" : @""];
  48. [self updateSegmentedControl];
  49. self.segmentedControl.selectedSegmentIndex = [self selectedIndex];
  50. self.segmentedControl.enabled = !self.rowDescriptor.isDisabled;
  51. }
  52. #pragma mark - KVO
  53. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  54. {
  55. if (object == self.textLabel && [keyPath isEqualToString:@"text"]){
  56. if ([[change objectForKey:NSKeyValueChangeKindKey] isEqualToNumber:@(NSKeyValueChangeSetting)]){
  57. [self.contentView setNeedsUpdateConstraints];
  58. }
  59. }
  60. }
  61. #pragma mark - Properties
  62. -(UISegmentedControl *)segmentedControl
  63. {
  64. if (_segmentedControl) return _segmentedControl;
  65. _segmentedControl = [UISegmentedControl autolayoutView];
  66. [_segmentedControl setContentHuggingPriority:500 forAxis:UILayoutConstraintAxisHorizontal];
  67. return _segmentedControl;
  68. }
  69. -(UILabel *)textLabel
  70. {
  71. if (_textLabel) return _textLabel;
  72. _textLabel = [UILabel autolayoutView];
  73. [_textLabel setContentCompressionResistancePriority:500
  74. forAxis:UILayoutConstraintAxisHorizontal];
  75. return _textLabel;
  76. }
  77. #pragma mark - Action
  78. -(void)valueChanged
  79. {
  80. self.rowDescriptor.value = [self.rowDescriptor.selectorOptions objectAtIndex:self.segmentedControl.selectedSegmentIndex];
  81. }
  82. #pragma mark - Helper
  83. -(NSArray *)getItems
  84. {
  85. NSMutableArray * result = [[NSMutableArray alloc] init];
  86. for (id option in self.rowDescriptor.selectorOptions)
  87. [result addObject:[option displayText]];
  88. return result;
  89. }
  90. -(void)updateSegmentedControl
  91. {
  92. [self.segmentedControl removeAllSegments];
  93. [[self getItems] enumerateObjectsUsingBlock:^(id object, NSUInteger idex, __unused BOOL *stop){
  94. [self.segmentedControl insertSegmentWithTitle:[object displayText] atIndex:idex animated:NO];
  95. }];
  96. }
  97. -(NSInteger)selectedIndex
  98. {
  99. if (self.rowDescriptor.value){
  100. for (id option in self.rowDescriptor.selectorOptions){
  101. if ([[option valueData] isEqual:[self.rowDescriptor.value valueData]]){
  102. return [self.rowDescriptor.selectorOptions indexOfObject:option];
  103. }
  104. }
  105. }
  106. return UISegmentedControlNoSegment;
  107. }
  108. #pragma mark - Layout Constraints
  109. -(void)updateConstraints
  110. {
  111. if (self.dynamicCustomConstraints){
  112. [self.contentView removeConstraints:self.dynamicCustomConstraints];
  113. }
  114. self.dynamicCustomConstraints = [NSMutableArray array];
  115. NSDictionary * views = @{@"segmentedControl": self.segmentedControl, @"textLabel": self.textLabel};
  116. if (self.textLabel.text.length > 0){
  117. [self.dynamicCustomConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[textLabel]-16-[segmentedControl]-|"
  118. options:NSLayoutFormatAlignAllCenterY
  119. metrics:0
  120. views:views]];
  121. [self.dynamicCustomConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-12-[textLabel]-12-|"
  122. options:0
  123. metrics:0
  124. views:views]];
  125. }
  126. else{
  127. [self.dynamicCustomConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[segmentedControl]-|"
  128. options:NSLayoutFormatAlignAllCenterY
  129. metrics:0
  130. views:views]];
  131. [self.dynamicCustomConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[segmentedControl]-|"
  132. options:0
  133. metrics:0
  134. views:views]];
  135. }
  136. [self.contentView addConstraints:self.dynamicCustomConstraints];
  137. [super updateConstraints];
  138. }
  139. -(void)dealloc
  140. {
  141. [self.textLabel removeObserver:self forKeyPath:@"text"];
  142. }
  143. @end