ParamTableViewCell.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ParamTableViewCell.h"
  17. static const NSUInteger kHInset = 10;
  18. static const NSUInteger kVInset = 4;
  19. @implementation ParamTableViewCell {
  20. UILabel *_label;
  21. UITextField *_textField;
  22. }
  23. @synthesize paramConfig = _paramConfig;
  24. - (instancetype)init {
  25. self = [super initWithStyle:UITableViewCellStyleDefault
  26. reuseIdentifier:NSStringFromClass(self.class)];
  27. if (self) {
  28. self.selectionStyle = UITableViewCellSelectionStyleNone;
  29. _label = [[UILabel alloc] init];
  30. _label.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
  31. _textField = [[UITextField alloc] init];
  32. _textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
  33. [self.contentView addSubview:_label];
  34. [self.contentView addSubview:_textField];
  35. [_textField addTarget:self
  36. action:@selector(onTextFieldValueChanged)
  37. forControlEvents:UIControlEventEditingChanged];
  38. [_textField addTarget:self
  39. action:@selector(onTextFieldDidEndOnExit)
  40. forControlEvents:UIControlEventEditingDidEndOnExit];
  41. }
  42. return self;
  43. }
  44. - (void)layoutSubviews {
  45. _label.frame = CGRectMake(kHInset, kVInset, self.contentView.frame.size.width - 2 * kHInset,
  46. (self.contentView.frame.size.height / 2) - 2 * kVInset);
  47. _textField.frame = CGRectMake(kHInset, (self.contentView.frame.size.height / 2) + kVInset,
  48. self.contentView.frame.size.width - 2 * kHInset,
  49. (self.contentView.frame.size.height / 2) - 2 * kVInset);
  50. }
  51. - (void)onTextFieldValueChanged {
  52. if (![self.textFieldValue isEqualToString:_textField.text]) {
  53. self.textFieldValue = _textField.text;
  54. [_delegate paramTableViewCellUpdatedValue:self];
  55. }
  56. }
  57. - (void)onTextFieldDidEndOnExit {
  58. [_textField resignFirstResponder];
  59. }
  60. - (void)setTextFieldValue:(NSString *)textFieldValue {
  61. _textFieldValue = textFieldValue;
  62. if (![_textFieldValue isEqualToString:_textField.text]) {
  63. _textField.text = self.textFieldValue;
  64. }
  65. }
  66. - (void)setParamConfig:(NSDictionary *)paramConfig {
  67. _paramConfig = [paramConfig copy];
  68. self.accessibilityIdentifier = _paramConfig[@"id"];
  69. _label.text = _paramConfig[@"label"];
  70. }
  71. @end