Skip to content

Commit ffbf80d

Browse files
author
Colin Regan
committed
Checks for button array equality to avoid removing and re-adding buttons if possible
1 parent 2850f57 commit ffbf80d

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

SWTableViewCell/PodFiles/NSMutableArray+SWUtilityButtons.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@
1515
- (void)sw_addUtilityButtonWithColor:(UIColor *)color normalIcon:(UIImage *)normalIcon selectedIcon:(UIImage *)selectedIcon;
1616

1717
@end
18+
19+
20+
@interface NSArray (SWUtilityButtons)
21+
22+
- (BOOL)sw_isEqualToButtons:(NSArray *)buttons;
23+
24+
@end

SWTableViewCell/PodFiles/NSMutableArray+SWUtilityButtons.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,45 @@ - (void)sw_addUtilityButtonWithColor:(UIColor *)color normalIcon:(UIImage *)norm
3838

3939
@end
4040

41+
42+
@implementation NSArray (SWUtilityButtons)
43+
44+
- (BOOL)sw_isEqualToButtons:(NSArray *)buttons
45+
{
46+
buttons = [buttons copy];
47+
if (!buttons || self.count != buttons.count) return NO;
48+
49+
for (NSUInteger idx = 0; idx < self.count; idx++) {
50+
id buttonA = self[idx];
51+
id buttonB = buttons[idx];
52+
if (![buttonA isKindOfClass:[UIButton class]] || ![buttonB isKindOfClass:[UIButton class]]) return NO;
53+
if (![[self class] sw_button:buttonA isEqualToButton:buttonB]) return NO;
54+
}
55+
56+
return YES;
57+
}
58+
59+
+ (BOOL)sw_button:(UIButton *)buttonA isEqualToButton:(UIButton *)buttonB
60+
{
61+
if (!buttonA || !buttonB) return NO;
62+
63+
UIColor *backgroundColorA = buttonA.backgroundColor;
64+
UIColor *backgroundColorB = buttonB.backgroundColor;
65+
BOOL haveEqualBackgroundColors = (!backgroundColorA && !backgroundColorB) || [backgroundColorA isEqual:backgroundColorB];
66+
67+
NSString *titleA = [buttonA titleForState:UIControlStateNormal];
68+
NSString *titleB = [buttonB titleForState:UIControlStateNormal];
69+
BOOL haveEqualTitles = (!titleA && !titleB) || [titleA isEqualToString:titleB];
70+
71+
UIImage *normalIconA = [buttonA imageForState:UIControlStateNormal];
72+
UIImage *normalIconB = [buttonB imageForState:UIControlStateNormal];
73+
BOOL haveEqualNormalIcons = (!normalIconA && !normalIconB) || [normalIconA isEqual:normalIconB];
74+
75+
UIImage *selectedIconA = [buttonA imageForState:UIControlStateSelected];
76+
UIImage *selectedIconB = [buttonB imageForState:UIControlStateSelected];
77+
BOOL haveEqualSelectedIcons = (!selectedIconA && !selectedIconB) || [selectedIconA isEqual:selectedIconB];
78+
79+
return haveEqualBackgroundColors && haveEqualTitles && haveEqualNormalIcons && haveEqualSelectedIcons;
80+
}
81+
82+
@end

SWTableViewCell/PodFiles/SWTableViewCell.m

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,24 @@ - (void)setContainingTableView:(UITableView *)containingTableView
175175

176176
- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons
177177
{
178-
_leftUtilityButtons = leftUtilityButtons;
179-
180-
self.leftUtilityButtonsView.utilityButtons = leftUtilityButtons;
181-
182-
[self layoutIfNeeded];
178+
if (![_leftUtilityButtons sw_isEqualToButtons:leftUtilityButtons]) {
179+
_leftUtilityButtons = leftUtilityButtons;
180+
181+
self.leftUtilityButtonsView.utilityButtons = leftUtilityButtons;
182+
183+
[self layoutIfNeeded];
184+
}
183185
}
184186

185187
- (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons
186188
{
187-
_rightUtilityButtons = rightUtilityButtons;
188-
189-
self.rightUtilityButtonsView.utilityButtons = rightUtilityButtons;
190-
191-
[self layoutIfNeeded];
189+
if (![_rightUtilityButtons sw_isEqualToButtons:rightUtilityButtons]) {
190+
_rightUtilityButtons = rightUtilityButtons;
191+
192+
self.rightUtilityButtonsView.utilityButtons = rightUtilityButtons;
193+
194+
[self layoutIfNeeded];
195+
}
192196
}
193197

194198
#pragma mark - UITableViewCell overrides

0 commit comments

Comments
 (0)