Skip to content

Commit 1950f35

Browse files
committed
Used fixed cell layout to make sure cellHeight is correclty calculated
Setting cells have different height, depending on their content and mode. To calculate this cell height correctly before heightForRowAtIndexPath is called, the whole layout needs to use the final size, which is fixed and represented by cellWidth. To keep this width, do not use autoresizing. For better centralization of the code, change dimensions only in configureCell and create all UI elements only using new.
1 parent 3cd4854 commit 1950f35

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

XBMC Remote/SettingsValuesViewController.m

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,12 @@ - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)
447447
return numRows;
448448
}
449449

450-
- (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
450+
- (void)configureCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
451451
cell.backgroundColor = [Utilities getSystemGray6];
452452
cell.tintColor = [Utilities getSystemBlue];
453453
cell.accessoryType = UITableViewCellAccessoryNone;
454+
455+
CGFloat cellWidth = IS_IPHONE ? GET_MAINSCREEN_WIDTH : STACKSCROLL_WIDTH;
454456

455457
UILabel *cellLabel = (UILabel*)[cell viewWithTag:SETTINGS_CELL_LABEL];
456458
UILabel *descriptionLabel = (UILabel*)[cell viewWithTag:SETTINGS_CELL_DESCRIPTION];
@@ -478,20 +480,20 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
478480
cellLabel.numberOfLines = 0;
479481
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
480482
PADDING_VERTICAL,
481-
cell.bounds.size.width - onoff.frame.size.width - 3 * PADDING_HORIZONTAL,
483+
cellWidth - onoff.frame.size.width - 3 * PADDING_HORIZONTAL,
482484
LABEL_HEIGHT_DEFAULT);
483485
[self setAutomaticLabelHeight:cellLabel];
484486

485487
onoff.on = [self.detailItem[@"value"] boolValue];
486-
onoff.frame = CGRectMake(cell.bounds.size.width - onoff.frame.size.width - PADDING_HORIZONTAL,
488+
onoff.frame = CGRectMake(cellWidth - onoff.frame.size.width - PADDING_HORIZONTAL,
487489
(CGRectGetHeight(cellLabel.frame) - CGRectGetHeight(onoff.frame)) / 2 + CGRectGetMinY(cellLabel.frame),
488490
CGRectGetWidth(onoff.frame),
489491
CGRectGetHeight(onoff.frame));
490492

491493
descriptionLabel.text = descriptionString;
492494
descriptionLabel.frame = CGRectMake(PADDING_HORIZONTAL,
493495
CGRectGetMaxY(cellLabel.frame) + PADDING_VERTICAL,
494-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
496+
cellWidth - 2 * PADDING_HORIZONTAL,
495497
LABEL_HEIGHT_DEFAULT);
496498
[self setAutomaticLabelHeight:descriptionLabel];
497499

@@ -509,6 +511,11 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
509511
else if ([settingOptions[indexPath.row][@"value"] isEqual:self.detailItem[@"value"]]) {
510512
cell.accessoryType = UITableViewCellAccessoryCheckmark;
511513
}
514+
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
515+
PADDING_VERTICAL,
516+
cellWidth - 2 * PADDING_HORIZONTAL,
517+
LABEL_HEIGHT_DEFAULT);
518+
cellHeight = CELL_HEIGHT_DEFAULT;
512519
break;
513520

514521
case SettingTypeSlider:
@@ -522,7 +529,7 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
522529
cellLabel.numberOfLines = 0;
523530
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
524531
PADDING_VERTICAL,
525-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
532+
cellWidth - 2 * PADDING_HORIZONTAL,
526533
LABEL_HEIGHT_DEFAULT);
527534
[self setAutomaticLabelHeight:cellLabel];
528535

@@ -531,24 +538,24 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
531538
descriptionLabel.numberOfLines = 0;
532539
descriptionLabel.frame = CGRectMake(PADDING_HORIZONTAL,
533540
CGRectGetMaxY(cellLabel.frame) + PADDING_VERTICAL,
534-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
541+
cellWidth - 2 * PADDING_HORIZONTAL,
535542
LABEL_HEIGHT_DEFAULT);
536543
[self setAutomaticLabelHeight:descriptionLabel];
537544

538545
sliderLabel.text = [self getStringForSliderItem:itemControls value:[self.detailItem[@"value"] floatValue]];
539-
sliderLabel.frame = CGRectMake(CGRectGetMinX(sliderLabel.frame),
546+
sliderLabel.frame = CGRectMake(SLIDER_PADDING,
540547
CGRectGetMaxY(descriptionLabel.frame) + 2 * PADDING_VERTICAL,
541-
CGRectGetWidth(sliderLabel.frame),
548+
cellWidth - 2 * SLIDER_PADDING,
542549
LABEL_HEIGHT_DEFAULT);
543550
[self setAutomaticLabelHeight:sliderLabel];
544551

545552
slider.minimumValue = [self.detailItem[@"minimum"] intValue];
546553
slider.maximumValue = [self.detailItem[@"maximum"] intValue];
547554
slider.value = [self.detailItem[@"value"] floatValue];
548-
slider.frame = CGRectMake(CGRectGetMinX(slider.frame),
555+
slider.frame = CGRectMake(SLIDER_PADDING,
549556
CGRectGetMaxY(sliderLabel.frame) + PADDING_VERTICAL,
550-
CGRectGetWidth(slider.frame),
551-
CGRectGetHeight(slider.frame));
557+
cellWidth - 2 * SLIDER_PADDING,
558+
SLIDER_HEIGHT);
552559

553560
cellHeight = CGRectGetMaxY(slider.frame) + 2 * PADDING_VERTICAL;
554561
break;
@@ -562,7 +569,7 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
562569
cellLabel.numberOfLines = 0;
563570
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
564571
PADDING_VERTICAL,
565-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
572+
cellWidth - 2 * PADDING_HORIZONTAL,
566573
LABEL_HEIGHT_DEFAULT);
567574
[self setAutomaticLabelHeight:cellLabel];
568575

@@ -571,15 +578,15 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
571578
descriptionLabel.numberOfLines = 0;
572579
descriptionLabel.frame = CGRectMake(PADDING_HORIZONTAL,
573580
CGRectGetMaxY(cellLabel.frame) + PADDING_VERTICAL,
574-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
581+
cellWidth - 2 * PADDING_HORIZONTAL,
575582
LABEL_HEIGHT_DEFAULT);
576583
[self setAutomaticLabelHeight:descriptionLabel];
577584

578585
textInputField.text = [NSString stringWithFormat:@"%@", self.detailItem[@"value"]];
579-
textInputField.frame = CGRectMake(CGRectGetMinX(textInputField.frame),
586+
textInputField.frame = CGRectMake(SLIDER_PADDING,
580587
CGRectGetMaxY(descriptionLabel.frame) + PADDING_VERTICAL,
581-
CGRectGetWidth(textInputField.frame),
582-
CGRectGetHeight(textInputField.frame));
588+
cellWidth - 2 * SLIDER_PADDING,
589+
TEXTFIELD_HEIGHT);
583590

584591
cellHeight = CGRectGetMaxY(textInputField.frame) + PADDING_VERTICAL;
585592
break;
@@ -610,7 +617,7 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
610617
cellLabel.numberOfLines = 0;
611618
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
612619
PADDING_VERTICAL,
613-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
620+
cellWidth - 2 * PADDING_HORIZONTAL,
614621
LABEL_HEIGHT_DEFAULT);
615622
[self setAutomaticLabelHeight:cellLabel];
616623

@@ -626,7 +633,7 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
626633
cellLabel.numberOfLines = 0;
627634
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
628635
PADDING_VERTICAL,
629-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
636+
cellWidth - 2 * PADDING_HORIZONTAL,
630637
LABEL_HEIGHT_DEFAULT);
631638
[self setAutomaticLabelHeight:cellLabel];
632639

@@ -643,7 +650,7 @@ - (void)layoutCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexP
643650
cellLabel.numberOfLines = 0;
644651
cellLabel.frame = CGRectMake(PADDING_HORIZONTAL,
645652
PADDING_VERTICAL,
646-
cell.bounds.size.width - 2 * PADDING_HORIZONTAL,
653+
cellWidth - 2 * PADDING_HORIZONTAL,
647654
LABEL_HEIGHT_DEFAULT);
648655
[self setAutomaticLabelHeight:cellLabel];
649656

@@ -681,48 +688,35 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
681688
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];
682689
if (cell == nil) {
683690
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableCellIdentifier];
684-
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(PADDING_HORIZONTAL,
685-
(CELL_HEIGHT_DEFAULT - LABEL_HEIGHT_DEFAULT) / 2,
686-
cell.frame.size.width - 2 * PADDING_HORIZONTAL,
687-
LABEL_HEIGHT_DEFAULT)];
691+
UILabel *cellLabel = [UILabel new];
688692
cellLabel.tag = SETTINGS_CELL_LABEL;
689693
cellLabel.font = [UIFont systemFontOfSize:16];
690694
cellLabel.adjustsFontSizeToFitWidth = YES;
691695
cellLabel.minimumScaleFactor = FONT_SCALING_MIN;
692696
cellLabel.textColor = [Utilities get1stLabelColor];
693697
cellLabel.highlightedTextColor = [Utilities get1stLabelColor];
694-
cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
695698
[cell.contentView addSubview:cellLabel];
696699

697700
UISwitch *onoff = [UISwitch new];
698701
onoff.tag = SETTINGS_CELL_ONOFF_SWITCH;
699-
onoff.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
700702
[onoff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
701703
[cell.contentView addSubview:onoff];
702704

703-
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(PADDING_HORIZONTAL,
704-
0,
705-
cell.frame.size.width - 2 * PADDING_HORIZONTAL,
706-
LABEL_HEIGHT_DEFAULT)];
705+
UILabel *descriptionLabel = [UILabel new];
707706
descriptionLabel.tag = SETTINGS_CELL_DESCRIPTION;
708707
descriptionLabel.font = [UIFont systemFontOfSize:14];
709708
descriptionLabel.adjustsFontSizeToFitWidth = YES;
710709
descriptionLabel.minimumScaleFactor = FONT_SCALING_MIN;
711710
descriptionLabel.numberOfLines = 0;
712711
descriptionLabel.textColor = [Utilities get2ndLabelColor];
713712
descriptionLabel.highlightedTextColor = [Utilities get2ndLabelColor];
714-
descriptionLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
715713
[cell.contentView addSubview:descriptionLabel];
716714

717-
OBSlider *slider = [[OBSlider alloc] initWithFrame:CGRectMake(SLIDER_PADDING,
718-
0,
719-
cell.frame.size.width - 2 * SLIDER_PADDING,
720-
SLIDER_HEIGHT)];
715+
OBSlider *slider = [OBSlider new];
721716
slider.tag = SETTINGS_CELL_SLIDER;
722717
slider.backgroundColor = UIColor.clearColor;
723718
slider.minimumTrackTintColor = KODI_BLUE_COLOR;
724719
slider.continuous = YES;
725-
slider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
726720
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
727721
[slider addTarget:self action:@selector(stopUpdateSlider:) forControlEvents:UIControlEventEditingDidEnd];
728722
[slider addTarget:self action:@selector(stopUpdateSlider:) forControlEvents:UIControlEventTouchCancel];
@@ -731,24 +725,17 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
731725
[slider addTarget:self action:@selector(startUpdateSlider:) forControlEvents:UIControlEventTouchDown];
732726
[cell.contentView addSubview:slider];
733727

734-
UILabel *sliderLabel = [[UILabel alloc] initWithFrame:CGRectMake(SLIDER_PADDING,
735-
0,
736-
cell.frame.size.width - 2 * SLIDER_PADDING,
737-
LABEL_HEIGHT_DEFAULT)];
728+
UILabel *sliderLabel = [UILabel new];
738729
sliderLabel.tag = SETTINGS_CELL_SLIDER_LABEL;
739730
sliderLabel.font = [UIFont systemFontOfSize:14];
740731
sliderLabel.adjustsFontSizeToFitWidth = YES;
741732
sliderLabel.minimumScaleFactor = FONT_SCALING_MIN;
742733
sliderLabel.textAlignment = NSTextAlignmentCenter;
743734
sliderLabel.textColor = [Utilities get2ndLabelColor];
744735
sliderLabel.highlightedTextColor = [Utilities get2ndLabelColor];
745-
sliderLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
746736
[cell.contentView addSubview:sliderLabel];
747737

748-
UITextField *textInputField = [[UITextField alloc] initWithFrame:CGRectMake(SLIDER_PADDING,
749-
0,
750-
cell.frame.size.width - 2 * SLIDER_PADDING,
751-
TEXTFIELD_HEIGHT)];
738+
UITextField *textInputField = [UITextField new];
752739
textInputField.tag = SETTINGS_CELL_TEXTFIELD;
753740
textInputField.borderStyle = UITextBorderStyleRoundedRect;
754741
textInputField.textAlignment = NSTextAlignmentCenter;
@@ -760,10 +747,9 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
760747
textInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
761748
textInputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
762749
textInputField.delegate = self;
763-
textInputField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
764750
[cell.contentView addSubview:textInputField];
765751
}
766-
[self layoutCell:cell forRowAtIndexPath:indexPath];
752+
[self configureCell:cell forRowAtIndexPath:indexPath];
767753
return cell;
768754
}
769755

0 commit comments

Comments
 (0)