123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // XLFormInlineSelectorCell.m
- // XLForm ( https://github.com/xmartlabs/XLForm )
- //
- // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "XLForm.h"
- #import "XLFormInlineSelectorCell.h"
- @interface XLFormInlineSelectorCell()
- @end
- @implementation XLFormInlineSelectorCell
- {
- UIColor * _beforeChangeColor;
- }
- - (BOOL)canBecomeFirstResponder
- {
- return YES;
- }
- -(BOOL)becomeFirstResponder
- {
- if (self.isFirstResponder){
- return [super becomeFirstResponder];
- }
- _beforeChangeColor = self.detailTextLabel.textColor;
- BOOL result = [super becomeFirstResponder];
- if (result){
- XLFormRowDescriptor * inlineRowDescriptor = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes][self.rowDescriptor.rowType]];
- UITableViewCell<XLFormDescriptorCell> * cell = [inlineRowDescriptor cellForFormController:self.formViewController];
- NSAssert([cell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)], @"inline cell must conform to XLFormInlineRowDescriptorCell");
- UITableViewCell<XLFormInlineRowDescriptorCell> * inlineCell = (UITableViewCell<XLFormInlineRowDescriptorCell> *)cell;
- inlineCell.inlineRowDescriptor = self.rowDescriptor;
- [self.rowDescriptor.sectionDescriptor addFormRow:inlineRowDescriptor afterRow:self.rowDescriptor];
- [self.formViewController ensureRowIsVisible:inlineRowDescriptor];
- }
- return result;
- }
- -(BOOL)resignFirstResponder
- {
- if (![self isFirstResponder]) {
- return [super resignFirstResponder];
- }
- NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
- NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
- XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
- XLFormSectionDescriptor * formSection = [self.formViewController.form.formSections objectAtIndex:nextRowPath.section];
- BOOL result = [super resignFirstResponder];
- if (result) {
- [formSection removeFormRow:nextFormRow];
- }
- return result;
- }
- #pragma mark - XLFormDescriptorCell
- -(void)configure
- {
- [super configure];
- }
- -(void)update
- {
- [super update];
- self.accessoryType = UITableViewCellAccessoryNone;
- self.editingAccessoryType = UITableViewCellAccessoryNone;
- [self.textLabel setText:self.rowDescriptor.title];
- self.selectionStyle = self.rowDescriptor.isDisabled ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
- self.textLabel.text = [NSString stringWithFormat:@"%@%@", self.rowDescriptor.title, self.rowDescriptor.required && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle ? @"*" : @""];
- self.detailTextLabel.text = [self valueDisplayText];
- }
- -(BOOL)formDescriptorCellCanBecomeFirstResponder
- {
- return !(self.rowDescriptor.isDisabled);
- }
- -(BOOL)formDescriptorCellBecomeFirstResponder
- {
- if ([self isFirstResponder]){
- [self resignFirstResponder];
- return NO;
- }
- return [self becomeFirstResponder];
- }
- -(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
- {
- [controller.tableView deselectRowAtIndexPath:[controller.form indexPathOfFormRow:self.rowDescriptor] animated:YES];
- }
- -(void)highlight
- {
- [super highlight];
- self.detailTextLabel.textColor = self.tintColor;
- }
- -(void)unhighlight
- {
- [super unhighlight];
- self.detailTextLabel.textColor = _beforeChangeColor;
- }
- #pragma mark - Helpers
- -(NSString *)valueDisplayText
- {
- if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelector] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelectorPopover]){
- if (!self.rowDescriptor.value || [self.rowDescriptor.value count] == 0){
- return self.rowDescriptor.noValueDisplayText;
- }
- if (self.rowDescriptor.valueTransformer){
- NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
- NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
- NSString * tranformedValue = [valueTransformer transformedValue:self.rowDescriptor.value];
- if (tranformedValue){
- return tranformedValue;
- }
- }
- NSMutableArray * descriptionArray = [NSMutableArray arrayWithCapacity:[self.rowDescriptor.value count]];
- for (id option in self.rowDescriptor.selectorOptions) {
- NSArray * selectedValues = self.rowDescriptor.value;
- if ([selectedValues formIndexForItem:option] != NSNotFound){
- if (self.rowDescriptor.valueTransformer){
- NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
- NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
- NSString * tranformedValue = [valueTransformer transformedValue:option];
- if (tranformedValue){
- [descriptionArray addObject:tranformedValue];
- }
- }
- else{
- [descriptionArray addObject:[option displayText]];
- }
- }
- }
- return [descriptionArray componentsJoinedByString:@", "];
- }
- if (!self.rowDescriptor.value){
- return self.rowDescriptor.noValueDisplayText;
- }
- if (self.rowDescriptor.valueTransformer){
- NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
- NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
- NSString * tranformedValue = [valueTransformer transformedValue:self.rowDescriptor.value];
- if (tranformedValue){
- return tranformedValue;
- }
- }
- return [self.rowDescriptor.value displayText];
- }
- @end
|