Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLSwipeForOptionsCell's buttons are now customizable #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions UITableViewCell-Swipe-for-Options/TLSwipeForOptionsCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@

@protocol TLSwipeForOptionsCellDelegate <NSObject>

-(void)cellDidSelectDelete:(TLSwipeForOptionsCell *)cell;
-(void)cellDidSelectMore:(TLSwipeForOptionsCell *)cell;
- (void)cell:(TLSwipeForOptionsCell*)cell didSelectButtonAtIndex:(NSUInteger)index withInfoDictionary:(NSDictionary*)info;

@end

extern NSString *const TLSwipeForOptionsCellEnclosingTableViewDidBeginScrollingNotification;

extern NSString *const TLSwipeForOptionsCellTitleOptionKey;
extern NSString *const TLSwipeForOptionsCellBackgroundColorOptionKey;
extern NSString *const TLSwipeForOptionsCellForegroundColorOptionKey;

@interface TLSwipeForOptionsCell : UITableViewCell

- (void)addButtonWithOptions:(NSDictionary*)options;
- (void)resetButtons;

@property (nonatomic, strong) NSMutableArray* buttons;
@property (nonatomic, weak) id<TLSwipeForOptionsCellDelegate> delegate;

@end
79 changes: 52 additions & 27 deletions UITableViewCell-Swipe-for-Options/TLSwipeForOptionsCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@

NSString *const TLSwipeForOptionsCellEnclosingTableViewDidBeginScrollingNotification = @"TLSwipeForOptionsCellEnclosingTableViewDidScrollNotification";

#define kCatchWidth 180
NSString *const TLSwipeForOptionsCellTitleOptionKey = @"TLSwipeForOptionsCellTitleOptionKey";
NSString *const TLSwipeForOptionsCellBackgroundColorOptionKey = @"TLSwipeForOptionsCellBackgroundColorOptionKey";
NSString *const TLSwipeForOptionsCellForegroundColorOptionKey = @"TLSwipeForOptionsCellForegroundColorOptionKey";

#define kButtonWidth 90.0
#define kCatchWidth (self.buttons.count * kButtonWidth)
#ifndef either
#define either(x,y) (x?x:y)
#endif

@interface TLSwipeForOptionsCell () <UIScrollViewDelegate>

Expand Down Expand Up @@ -40,7 +48,30 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
return self;
}

-(void)setup {
- (void)setupButtons {
[self.scrollViewButtonView.subviews.copy makeObjectsPerformSelector:@selector(removeFromSuperview)];

// for (NSDictionary* buttonDict in self.buttons) {
[self.buttons enumerateObjectsUsingBlock:^(NSDictionary* buttonDict, NSUInteger idx, BOOL *stop) {
// Set up our two buttons
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(kButtonWidth * idx, 0, kButtonWidth, CGRectGetHeight(self.bounds));
[aButton setTitle:buttonDict[TLSwipeForOptionsCellTitleOptionKey] forState:UIControlStateNormal];
[aButton setTitleColor:either(buttonDict[TLSwipeForOptionsCellForegroundColorOptionKey], UIColor.whiteColor)
forState:UIControlStateNormal];
aButton.backgroundColor = either(buttonDict[TLSwipeForOptionsCellBackgroundColorOptionKey],
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0f]);
aButton.tag = 2048 + idx;
[aButton addTarget:self action:@selector(userPressedButton:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollViewButtonView addSubview:aButton];
}];

self.scrollViewButtonView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + kCatchWidth, CGRectGetHeight(self.bounds));
[self setNeedsLayout];
}

- (void)setup {
// Set up our contentView hierarchy

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
Expand All @@ -54,24 +85,9 @@ -(void)setup {
UIView *scrollViewButtonView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - kCatchWidth, 0, kCatchWidth, CGRectGetHeight(self.bounds))];
self.scrollViewButtonView = scrollViewButtonView;
[self.scrollView addSubview:scrollViewButtonView];

// Set up our two buttons
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.backgroundColor = [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0f];
moreButton.frame = CGRectMake(0, 0, kCatchWidth / 2.0f, CGRectGetHeight(self.bounds));
[moreButton setTitle:@"More" forState:UIControlStateNormal];
[moreButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[moreButton addTarget:self action:@selector(userPressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollViewButtonView addSubview:moreButton];

UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.backgroundColor = [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0f];
deleteButton.frame = CGRectMake(kCatchWidth / 2.0f, 0, kCatchWidth / 2.0f, CGRectGetHeight(self.bounds));
[deleteButton setTitle:@"Delete" forState:UIControlStateNormal];
[deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[deleteButton addTarget:self action:@selector(userPressedDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollViewButtonView addSubview:deleteButton];


[self setupButtons];

UIView *scrollViewContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
scrollViewContentView.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:scrollViewContentView];
Expand All @@ -88,17 +104,25 @@ -(void)enclosingTableViewDidScroll {
[self.scrollView setContentOffset:CGPointZero animated:YES];
}

- (void)addButtonWithOptions:(NSDictionary *)options {
if (!self.buttons)
self.buttons = NSMutableArray.new;
[self.buttons addObject:options];
[self setupButtons];
}
- (void)resetButtons {
self.buttons = nil;
}

#pragma mark - Private Methods

-(void)userPressedDeleteButton:(id)sender {
[self.delegate cellDidSelectDelete:self];
-(void)userPressedButton:(id)sender {
NSUInteger buttonIndex = [sender tag] - 2048;
NSDictionary* buttonInfoDictionary = self.buttons[buttonIndex];
[self.delegate cell:self didSelectButtonAtIndex:buttonIndex withInfoDictionary:buttonInfoDictionary];
[self.scrollView setContentOffset:CGPointZero animated:YES];
}

-(void)userPressedMoreButton:(id)sender {
[self.delegate cellDidSelectMore:self];
}

#pragma mark - Overridden Methods

-(void)layoutSubviews {
Expand All @@ -112,7 +136,7 @@ -(void)layoutSubviews {

-(void)prepareForReuse {
[super prepareForReuse];

[self resetButtons];
[self.scrollView setContentOffset:CGPointZero animated:NO];
}

Expand Down Expand Up @@ -160,3 +184,4 @@ -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
@end

#undef kCatchWidth
#undef kButtonWidth
29 changes: 19 additions & 10 deletions UITableViewCell-Swipe-for-Options/TLTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
cell.delegate = self;
// [cell resetButtons]; // prepareForReuse function already does this, hopefully.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to remove this than comment it out.

[cell addButtonWithOptions:@{
TLSwipeForOptionsCellTitleOptionKey: @"More",
TLSwipeForOptionsCellBackgroundColorOptionKey:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0f]
}];
[cell addButtonWithOptions:@{
TLSwipeForOptionsCellTitleOptionKey: @"Delete",
TLSwipeForOptionsCellBackgroundColorOptionKey:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0f]
}];

return cell;
}
Expand Down Expand Up @@ -108,17 +119,15 @@ -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

#pragma mark - TLSwipeForOptionsCellDelegate Methods

-(void)cellDidSelectDelete:(TLSwipeForOptionsCell *)cell {
- (void)cell:(TLSwipeForOptionsCell *)cell didSelectButtonAtIndex:(NSUInteger)index withInfoDictionary:(NSDictionary *)info {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[_objects removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)cellDidSelectMore:(TLSwipeForOptionsCell *)cell {
self.mostRecentlySelectedMoreCell = cell;
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Flag", @"Mark as Unread", @"Move to Junk", @"Move Messages...", nil];
[self.delegate presentActionSheet:actionSheet fromViewController:self];
if (index == 0) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Flag", @"Mark as Unread", @"Move to Junk", @"Move Messages...", nil];
[self.delegate presentActionSheet:actionSheet fromViewController:self];
} else {
[_objects removeObjectAtIndex:indexPath.row];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use self.objects instead of referring to the ivar directly?

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
Expand Down