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

Added public API to show the left and right utility buttons programmatically (related to #97). #133

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions SWTableViewCell/PodFiles/SWTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef NS_ENUM(NSInteger, SWCellState)

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier containingTableView:(UITableView *)containingTableView leftUtilityButtons:(NSArray *)leftUtilityButtons rightUtilityButtons:(NSArray *)rightUtilityButtons;

- (void)showLeftUtilityButtonsAnimated:(BOOL)animated;
- (void)showRightUtilityButtonsAnimated:(BOOL)animated;
- (void)hideUtilityButtonsAnimated:(BOOL)animated;

@end
84 changes: 52 additions & 32 deletions SWTableViewCell/PodFiles/SWTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (CGFloat)utilityButtonsPadding;

- (CGPoint)contentOffsetForCellState:(SWCellState)state;
- (void)updateCellState;
- (void)finishScrollingToCellState:(SWCellState)targetState;

- (BOOL)shouldHighlight;

Expand Down Expand Up @@ -198,6 +199,8 @@ - (void)setContainingTableView:(UITableView *)containingTableView
}
}

#pragma mark - Public API

- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons
{
_leftUtilityButtons = leftUtilityButtons;
Expand All @@ -216,6 +219,24 @@ - (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons
[self layoutIfNeeded];
}

- (void)showLeftUtilityButtonsAnimated:(BOOL)animated
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateLeft] animated:animated];
[self finishScrollingToCellState:kCellStateLeft];
}

- (void)showRightUtilityButtonsAnimated:(BOOL)animated
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateRight] animated:animated];
[self finishScrollingToCellState:kCellStateRight];
}

- (void)hideUtilityButtonsAnimated:(BOOL)animated
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateCenter] animated:animated];
[self finishScrollingToCellState:kCellStateCenter];
}

#pragma mark - UITableViewCell overrides

- (void)didMoveToSuperview
Expand Down Expand Up @@ -395,20 +416,31 @@ - (void)leftUtilityButtonHandler:(id)sender
}
}

- (void)hideUtilityButtonsAnimated:(BOOL)animated
- (void)finishScrollingToCellState:(SWCellState)targetState
{
if (_cellState != kCellStateCenter)
if (_cellState != targetState)
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateCenter] animated:YES];

if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateCenter];
[self.delegate swipeableTableViewCell:self scrollingToState:targetState];
}

if (targetState != kCellStateCenter)
{
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:)])
{
for (SWTableViewCell *cell in self.containingTableView.visibleCells)
{
if (cell != self && [cell isKindOfClass:[SWTableViewCell class]] && [self.delegate swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:cell])
{
[cell hideUtilityButtonsAnimated:YES];
}
}
}
}
}
}


#pragma mark - Geometry helpers

- (CGFloat)leftUtilityButtonsWidth
Expand Down Expand Up @@ -490,26 +522,28 @@ - (void)updateCellState

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
SWCellState targetState;

if (velocity.x >= 0.5f)
{
if (_cellState == kCellStateLeft)
{
_cellState = kCellStateCenter;
targetState = kCellStateCenter;
}
else
{
_cellState = kCellStateRight;
targetState = kCellStateRight;
}
}
else if (velocity.x <= -0.5f)
{
if (_cellState == kCellStateRight)
{
_cellState = kCellStateCenter;
targetState = kCellStateCenter;
}
else
{
_cellState = kCellStateLeft;
targetState = kCellStateLeft;
}
}
else
Expand All @@ -519,36 +553,22 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi

if (targetContentOffset->x > rightThreshold)
{
_cellState = kCellStateRight;
targetState = kCellStateRight;
}
else if (targetContentOffset->x < leftThreshold)
{
_cellState = kCellStateLeft;
targetState = kCellStateLeft;
}
else
{
_cellState = kCellStateCenter;
targetState = kCellStateCenter;
}
}

if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:_cellState];
}

if (_cellState != kCellStateCenter)
{
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:)])
{
for (SWTableViewCell *cell in [self.containingTableView visibleCells]) {
if (cell != self && [cell isKindOfClass:[SWTableViewCell class]] && [self.delegate swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:cell]) {
[cell hideUtilityButtonsAnimated:YES];
}
}
}
}

*targetContentOffset = [self contentOffsetForCellState:_cellState];

*targetContentOffset = [self contentOffsetForCellState:targetState];

// Fire the delegate messages and hide other open buttons if required.
[self finishScrollingToCellState:targetState];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Expand Down
139 changes: 69 additions & 70 deletions SWTableViewCell/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ @interface ViewController () {
}

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) IBOutlet UISwitch *tapToRevealSwitch;
@property (nonatomic) BOOL useCustomCells;
@property (nonatomic, weak) UIRefreshControl *refreshControl;

- (IBAction)toggleCells:(id)sender;

- (NSArray *)leftButtons;
- (NSArray *)rightButtons;

@end

@implementation ViewController
Expand Down Expand Up @@ -61,38 +67,42 @@ - (void)viewDidLoad {
}
}

#pragma mark UITableViewDataSource
#pragma mark -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _testArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_testArray[section] count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cell selected at index path %ld:%ld", (long)indexPath.section, (long)indexPath.row);
NSLog(@"selected cell index path is %@", [self.tableView indexPathForSelectedRow]);
- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
title:@"More"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Delete"];

[tableView deselectRowAtIndexPath:indexPath animated:YES];
return rightUtilityButtons;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return _sections[section];
- (NSArray *)leftButtons
{
NSMutableArray *leftUtilityButtons = [NSMutableArray new];

[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
icon:[UIImage imageNamed:@"check.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]
icon:[UIImage imageNamed:@"clock.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]
icon:[UIImage imageNamed:@"cross.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]
icon:[UIImage imageNamed:@"list.png"]];

return leftUtilityButtons;
}

// Show index titles

//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
//}
//
//- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
// return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
//}

#pragma mark - UIRefreshControl Selector
#pragma mark - IBActions

- (void)toggleCells:(UIRefreshControl*)refreshControl
{
Expand All @@ -110,7 +120,19 @@ - (void)toggleCells:(UIRefreshControl*)refreshControl
[refreshControl endRefreshing];
}

#pragma mark - UIScrollViewDelegate
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _testArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_testArray[section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return _sections[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Expand Down Expand Up @@ -145,59 +167,36 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.text = @"Some detail text";

return cell;
}

}

- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
title:@"More"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Delete"];

return rightUtilityButtons;
}
#pragma mark - UITableViewDelegate

- (NSArray *)leftButtons
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *leftUtilityButtons = [NSMutableArray new];

[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
icon:[UIImage imageNamed:@"check.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]
icon:[UIImage imageNamed:@"clock.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]
icon:[UIImage imageNamed:@"cross.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]
icon:[UIImage imageNamed:@"list.png"]];

return leftUtilityButtons;
}

// Set row height on an individual basis
NSLog(@"cell selected at index path %ld:%ld", (long)indexPath.section, (long)indexPath.row);
NSLog(@"selected cell index path is %@", [self.tableView indexPathForSelectedRow]);

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// return [self rowHeightForIndexPath:indexPath];
//}
//
//- (CGFloat)rowHeightForIndexPath:(NSIndexPath *)indexPath {
// return ([indexPath row] * 10) + 60;
//}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Set background color of cell here if you don't want default white
if (self.tapToRevealSwitch.isOn)
{
SWTableViewCell *cell = (SWTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

if (arc4random() % 2)
{
[cell showLeftUtilityButtonsAnimated:YES];
}
else
{
[cell showRightUtilityButtonsAnimated:YES];
}
}
}


#pragma mark - SWTableViewDelegate

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index {
Expand Down
Loading