Skip to content

Commit aaae62c

Browse files
committed
- (UIButton *)swipeableCell:(DNSSwipeableCell *)cell buttonForIndex:(NSInteger)index added
1 parent 8f73d58 commit aaae62c

File tree

3 files changed

+93
-49
lines changed

3 files changed

+93
-49
lines changed

DNSSwipeableCell/MasterViewController.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ - (NSInteger)numberOfButtonsInSwipeableCell:(DNSSwipeableCell *)cell
167167
// return [UIFont fontWithName:@"AmericanTypewriter" size:14.0f];
168168
//}
169169

170+
//Uncomment to show fully custom button
171+
//- (UIButton *)swipeableCell:(DNSSwipeableCell *)cell buttonForIndex:(NSInteger)index
172+
//{
173+
// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
174+
// [button setTitle:@"Custom" forState:UIControlStateNormal];
175+
// [button setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 10.0f)];
176+
// [button setBackgroundColor:[UIColor redColor]];
177+
//
178+
// return button;
179+
//}
180+
170181
- (NSString *)swipeableCell:(DNSSwipeableCell *)cell titleForButtonAtIndex:(NSInteger)index
171182
{
172183
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];

Library/DNSSwipeableCell.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,26 @@
2626

2727
@optional
2828

29+
/**
30+
* The button at a given index in a particular swipeable cell.
31+
*
32+
* REMEMBER: Button indexes will be right to left since that's the way the cell
33+
* slides open - for example | index 2 index 1 index 0 |.
34+
*
35+
* @param cell - The cell for which to find the button title
36+
* @param index - The index of the button for which this string should be the title.
37+
* @return The UIButton to display
38+
*/
39+
- (UIButton *)swipeableCell:(DNSSwipeableCell *)cell buttonForIndex:(NSInteger)index;
40+
2941
/**
3042
* The title for the button at a given index in a particular swipeable cell. Defaults to
3143
* an empty string.
3244
*
3345
* REMEMBER: Button indexes will be right to left since that's the way the cell
3446
* slides open - for example | index 2 index 1 index 0 |.
47+
*
48+
* Does nothing if -swipeableCell: buttonForIndex: implemented.
3549
*
3650
* @param cell - The cell for which to find the button title
3751
* @param index - The index of the button for which this string should be the title.
@@ -47,6 +61,8 @@
4761
* REMEMBER: Button indexes will be right to left since that's the way the cell
4862
* slides open - for example | index 2 index 1 index 0 |.
4963
*
64+
* Does nothing if -swipeableCell: buttonForIndex: implemented.
65+
*
5066
* @param cell - The cell for which to find the button background
5167
* @param index - The index of the button for which this color should be the backgroundColor.
5268
* @return The background color you wish to display.
@@ -60,6 +76,8 @@
6076
* REMEMBER: Button indexes will be right to left since that's the way the cell
6177
* slides open - for example | index 2 index 1 index 0 |.
6278
*
79+
* Does nothing if -swipeableCell: buttonForIndex: implemented.
80+
*
6381
* @param cell - The cell for which to find the button tintColor
6482
* @param index - The index of the button for which this color should be the text color.
6583
* @return The text color you wish to display.
@@ -73,6 +91,8 @@
7391
* REMEMBER: Button indexes will be right to left since that's the way the cell
7492
* slides open - for example | index 2 index 1 index 0 |.
7593
*
94+
* Does nothing if -swipeableCell: buttonForIndex: implemented.
95+
*
7696
* @param cell - The cell for which you wish to find the button image.
7797
* @param index - The index of the button for which this should be the image.
7898
* @return The Image you wish to display.
@@ -86,6 +106,8 @@
86106
* REMEMBER: Button indexes will be right to left since that's the way the cell
87107
* slides open - for example | index 2 index 1 index 0 |.
88108
*
109+
* Does nothing if -swipeableCell: buttonForIndex: implemented.
110+
*
89111
* @param cell - The cell for which you wish to find the font.
90112
* @param index - The index of the button for which this should be the font.
91113
* @return The font you wish to display.

Library/DNSSwipeableCell.m

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -138,60 +138,71 @@ - (void)configureButtonsIfNeeded
138138

139139
- (UIButton *)buttonForIndex:(NSInteger)index previousButtonMinX:(CGFloat)previousMinX
140140
{
141-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
141+
UIButton *button = nil;
142142

143-
if ([self.dataSource respondsToSelector:@selector(swipeableCell:backgroundColorForButtonAtIndex:)]) {
144-
//Set background color from data source
145-
button.backgroundColor = [self.dataSource swipeableCell:self backgroundColorForButtonAtIndex:index];
143+
/* The datasource implements buttonForIndex. Let's use custom buttom */
144+
if ([self.dataSource respondsToSelector:@selector(swipeableCell:buttonForIndex:)]) {
145+
146+
button = [self.dataSource swipeableCell:self buttonForIndex:index];
147+
148+
/* Lets generate the button */
146149
} else {
147-
//Use default colors
148-
if (index == 0) {
149-
button.backgroundColor = [UIColor redColor];
150+
151+
button = [UIButton buttonWithType:UIButtonTypeCustom];
152+
153+
if ([self.dataSource respondsToSelector:@selector(swipeableCell:titleForButtonAtIndex:)]) {
154+
//use given title
155+
[button setTitle:[self.dataSource swipeableCell:self titleForButtonAtIndex:index] forState:UIControlStateNormal];
150156
} else {
151-
button.backgroundColor = [UIColor lightGrayColor];
157+
//Default to empty title
158+
[button setTitle:@"" forState:UIControlStateNormal];
152159
}
153-
}
154-
155-
if ([self.dataSource respondsToSelector:@selector(swipeableCell:titleForButtonAtIndex:)]) {
156-
//use given title
157-
[button setTitle:[self.dataSource swipeableCell:self titleForButtonAtIndex:index] forState:UIControlStateNormal];
158-
} else {
159-
//Default to empty title
160-
[button setTitle:@"" forState:UIControlStateNormal];
161-
}
162-
163-
if ([self.dataSource respondsToSelector:@selector(swipeableCell:imageForButtonAtIndex:)]) {
164-
//Use the image, if it exists
165-
UIImage *iconImage = [self.dataSource swipeableCell:self imageForButtonAtIndex:index];
166-
if (iconImage) {
167-
[button setImage:[iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
160+
161+
if ([self.dataSource respondsToSelector:@selector(swipeableCell:imageForButtonAtIndex:)]) {
162+
//Use the image, if it exists
163+
UIImage *iconImage = [self.dataSource swipeableCell:self imageForButtonAtIndex:index];
164+
if (iconImage) {
165+
[button setImage:[iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
166+
}
167+
}
168+
169+
if ([self.dataSource respondsToSelector:@selector(swipeableCell:tintColorForButtonAtIndex:)]) {
170+
//Use the given tint color.
171+
button.tintColor = [self.dataSource swipeableCell:self tintColorForButtonAtIndex:index];
172+
} else {
173+
//Default to white
174+
button.tintColor = [UIColor whiteColor];
175+
}
176+
177+
//Add 8pt of padding on the left and right.
178+
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 8)];
179+
180+
if ([self.dataSource respondsToSelector:@selector(swipeableCell:fontForButtonAtIndex:)]) {
181+
//Set font if provided.
182+
button.titleLabel.font = [self.dataSource swipeableCell:self fontForButtonAtIndex:index];
183+
}
184+
185+
//Size the button to fit the contents
186+
[button sizeToFit];
187+
188+
CGFloat appleRecommendedMinimumTouchPointWidth = 44.0f;
189+
if (button.frame.size.width < appleRecommendedMinimumTouchPointWidth) {
190+
CGRect frame = button.frame;
191+
frame.size.width = appleRecommendedMinimumTouchPointWidth;
192+
button.frame = frame;
193+
}
194+
195+
if ([self.dataSource respondsToSelector:@selector(swipeableCell:backgroundColorForButtonAtIndex:)]) {
196+
//Set background color from data source
197+
button.backgroundColor = [self.dataSource swipeableCell:self backgroundColorForButtonAtIndex:index];
198+
} else {
199+
//Use default colors
200+
if (index == 0) {
201+
button.backgroundColor = [UIColor redColor];
202+
} else {
203+
button.backgroundColor = [UIColor lightGrayColor];
204+
}
168205
}
169-
}
170-
171-
if ([self.dataSource respondsToSelector:@selector(swipeableCell:tintColorForButtonAtIndex:)]) {
172-
//Use the given tint color.
173-
button.tintColor = [self.dataSource swipeableCell:self tintColorForButtonAtIndex:index];
174-
} else {
175-
//Default to white
176-
button.tintColor = [UIColor whiteColor];
177-
}
178-
179-
//Add 8pt of padding on the left and right.
180-
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 8)];
181-
182-
if ([self.dataSource respondsToSelector:@selector(swipeableCell:fontForButtonAtIndex:)]) {
183-
//Set font if provided.
184-
button.titleLabel.font = [self.dataSource swipeableCell:self fontForButtonAtIndex:index];
185-
}
186-
187-
//Size the button to fit the contents
188-
[button sizeToFit];
189-
190-
CGFloat appleRecommendedMinimumTouchPointWidth = 44.0f;
191-
if (button.frame.size.width < appleRecommendedMinimumTouchPointWidth) {
192-
CGRect frame = button.frame;
193-
frame.size.width = appleRecommendedMinimumTouchPointWidth;
194-
button.frame = frame;
195206
}
196207

197208
//Update the origin and size to make sure that everything is the size it needs to be

0 commit comments

Comments
 (0)