Skip to content

Commit

Permalink
Merge pull request #10 from Krivoblotsky/master
Browse files Browse the repository at this point in the history
Ability to show custom buttons added.
  • Loading branch information
designatednerd committed Sep 24, 2014
2 parents a0aa7bb + aaae62c commit ad8fea8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 49 deletions.
11 changes: 11 additions & 0 deletions DNSSwipeableCell/MasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ - (NSInteger)numberOfButtonsInSwipeableCell:(DNSSwipeableCell *)cell
// return [UIFont fontWithName:@"AmericanTypewriter" size:14.0f];
//}

//Uncomment to show fully custom button
//- (UIButton *)swipeableCell:(DNSSwipeableCell *)cell buttonForIndex:(NSInteger)index
//{
// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// [button setTitle:@"Custom" forState:UIControlStateNormal];
// [button setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 10.0f)];
// [button setBackgroundColor:[UIColor redColor]];
//
// return button;
//}

- (NSString *)swipeableCell:(DNSSwipeableCell *)cell titleForButtonAtIndex:(NSInteger)index
{
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];
Expand Down
22 changes: 22 additions & 0 deletions Library/DNSSwipeableCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,26 @@

@optional

/**
* The button at a given index in a particular swipeable cell.
*
* REMEMBER: Button indexes will be right to left since that's the way the cell
* slides open - for example | index 2 index 1 index 0 |.
*
* @param cell - The cell for which to find the button title
* @param index - The index of the button for which this string should be the title.
* @return The UIButton to display
*/
- (UIButton *)swipeableCell:(DNSSwipeableCell *)cell buttonForIndex:(NSInteger)index;

/**
* The title for the button at a given index in a particular swipeable cell. Defaults to
* an empty string.
*
* REMEMBER: Button indexes will be right to left since that's the way the cell
* slides open - for example | index 2 index 1 index 0 |.
*
* Does nothing if -swipeableCell: buttonForIndex: implemented.
*
* @param cell - The cell for which to find the button title
* @param index - The index of the button for which this string should be the title.
Expand All @@ -47,6 +61,8 @@
* REMEMBER: Button indexes will be right to left since that's the way the cell
* slides open - for example | index 2 index 1 index 0 |.
*
* Does nothing if -swipeableCell: buttonForIndex: implemented.
*
* @param cell - The cell for which to find the button background
* @param index - The index of the button for which this color should be the backgroundColor.
* @return The background color you wish to display.
Expand All @@ -60,6 +76,8 @@
* REMEMBER: Button indexes will be right to left since that's the way the cell
* slides open - for example | index 2 index 1 index 0 |.
*
* Does nothing if -swipeableCell: buttonForIndex: implemented.
*
* @param cell - The cell for which to find the button tintColor
* @param index - The index of the button for which this color should be the text color.
* @return The text color you wish to display.
Expand All @@ -73,6 +91,8 @@
* REMEMBER: Button indexes will be right to left since that's the way the cell
* slides open - for example | index 2 index 1 index 0 |.
*
* Does nothing if -swipeableCell: buttonForIndex: implemented.
*
* @param cell - The cell for which you wish to find the button image.
* @param index - The index of the button for which this should be the image.
* @return The Image you wish to display.
Expand All @@ -86,6 +106,8 @@
* REMEMBER: Button indexes will be right to left since that's the way the cell
* slides open - for example | index 2 index 1 index 0 |.
*
* Does nothing if -swipeableCell: buttonForIndex: implemented.
*
* @param cell - The cell for which you wish to find the font.
* @param index - The index of the button for which this should be the font.
* @return The font you wish to display.
Expand Down
109 changes: 60 additions & 49 deletions Library/DNSSwipeableCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,60 +140,71 @@ - (void)configureButtonsIfNeeded

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

if ([self.dataSource respondsToSelector:@selector(swipeableCell:backgroundColorForButtonAtIndex:)]) {
//Set background color from data source
button.backgroundColor = [self.dataSource swipeableCell:self backgroundColorForButtonAtIndex:index];
/* The datasource implements buttonForIndex. Let's use custom buttom */
if ([self.dataSource respondsToSelector:@selector(swipeableCell:buttonForIndex:)]) {

button = [self.dataSource swipeableCell:self buttonForIndex:index];

/* Lets generate the button */
} else {
//Use default colors
if (index == 0) {
button.backgroundColor = [UIColor redColor];

button = [UIButton buttonWithType:UIButtonTypeCustom];

if ([self.dataSource respondsToSelector:@selector(swipeableCell:titleForButtonAtIndex:)]) {
//use given title
[button setTitle:[self.dataSource swipeableCell:self titleForButtonAtIndex:index] forState:UIControlStateNormal];
} else {
button.backgroundColor = [UIColor lightGrayColor];
//Default to empty title
[button setTitle:@"" forState:UIControlStateNormal];
}
}

if ([self.dataSource respondsToSelector:@selector(swipeableCell:titleForButtonAtIndex:)]) {
//use given title
[button setTitle:[self.dataSource swipeableCell:self titleForButtonAtIndex:index] forState:UIControlStateNormal];
} else {
//Default to empty title
[button setTitle:@"" forState:UIControlStateNormal];
}

if ([self.dataSource respondsToSelector:@selector(swipeableCell:imageForButtonAtIndex:)]) {
//Use the image, if it exists
UIImage *iconImage = [self.dataSource swipeableCell:self imageForButtonAtIndex:index];
if (iconImage) {
[button setImage:[iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];

if ([self.dataSource respondsToSelector:@selector(swipeableCell:imageForButtonAtIndex:)]) {
//Use the image, if it exists
UIImage *iconImage = [self.dataSource swipeableCell:self imageForButtonAtIndex:index];
if (iconImage) {
[button setImage:[iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
}
}

if ([self.dataSource respondsToSelector:@selector(swipeableCell:tintColorForButtonAtIndex:)]) {
//Use the given tint color.
button.tintColor = [self.dataSource swipeableCell:self tintColorForButtonAtIndex:index];
} else {
//Default to white
button.tintColor = [UIColor whiteColor];
}

//Add 8pt of padding on the left and right.
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 8)];

if ([self.dataSource respondsToSelector:@selector(swipeableCell:fontForButtonAtIndex:)]) {
//Set font if provided.
button.titleLabel.font = [self.dataSource swipeableCell:self fontForButtonAtIndex:index];
}

//Size the button to fit the contents
[button sizeToFit];

CGFloat appleRecommendedMinimumTouchPointWidth = 44.0f;
if (button.frame.size.width < appleRecommendedMinimumTouchPointWidth) {
CGRect frame = button.frame;
frame.size.width = appleRecommendedMinimumTouchPointWidth;
button.frame = frame;
}

if ([self.dataSource respondsToSelector:@selector(swipeableCell:backgroundColorForButtonAtIndex:)]) {
//Set background color from data source
button.backgroundColor = [self.dataSource swipeableCell:self backgroundColorForButtonAtIndex:index];
} else {
//Use default colors
if (index == 0) {
button.backgroundColor = [UIColor redColor];
} else {
button.backgroundColor = [UIColor lightGrayColor];
}
}
}

if ([self.dataSource respondsToSelector:@selector(swipeableCell:tintColorForButtonAtIndex:)]) {
//Use the given tint color.
button.tintColor = [self.dataSource swipeableCell:self tintColorForButtonAtIndex:index];
} else {
//Default to white
button.tintColor = [UIColor whiteColor];
}

//Add 8pt of padding on the left and right.
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 8)];

if ([self.dataSource respondsToSelector:@selector(swipeableCell:fontForButtonAtIndex:)]) {
//Set font if provided.
button.titleLabel.font = [self.dataSource swipeableCell:self fontForButtonAtIndex:index];
}

//Size the button to fit the contents
[button sizeToFit];

CGFloat appleRecommendedMinimumTouchPointWidth = 44.0f;
if (button.frame.size.width < appleRecommendedMinimumTouchPointWidth) {
CGRect frame = button.frame;
frame.size.width = appleRecommendedMinimumTouchPointWidth;
button.frame = frame;
}

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

0 comments on commit ad8fea8

Please sign in to comment.