Skip to content

Commit ad8fea8

Browse files
Merge pull request #10 from Krivoblotsky/master
Ability to show custom buttons added.
2 parents a0aa7bb + aaae62c commit ad8fea8

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
@@ -140,60 +140,71 @@ - (void)configureButtonsIfNeeded
140140

141141
- (UIButton *)buttonForIndex:(NSInteger)index previousButtonMinX:(CGFloat)previousMinX
142142
{
143-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
143+
UIButton *button = nil;
144144

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

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

0 commit comments

Comments
 (0)