diff --git a/SWTableViewCell/SWTableViewCell.h b/SWTableViewCell/SWTableViewCell.h index 082bd55..4a751e4 100644 --- a/SWTableViewCell/SWTableViewCell.h +++ b/SWTableViewCell/SWTableViewCell.h @@ -7,6 +7,7 @@ // #import +#import @class SWTableViewCell; diff --git a/SWTableViewCell/SWTableViewCell.m b/SWTableViewCell/SWTableViewCell.m index e8d67e8..e5f6f57 100644 --- a/SWTableViewCell/SWTableViewCell.m +++ b/SWTableViewCell/SWTableViewCell.m @@ -19,6 +19,7 @@ kCellStateRight } SWCellState; + #pragma mark - SWUtilityButtonView @interface SWUtilityButtonView : UIView @@ -45,7 +46,7 @@ - (id)initWithUtilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCe self.utilityButtons = utilityButtons; self.utilityButtonWidth = [self calculateUtilityButtonWidth]; self.parentCell = parentCell; - self.utilityButtonSelector = utilityButtonSelector; // eh. + self.utilityButtonSelector = utilityButtonSelector; } return self; @@ -58,7 +59,7 @@ - (id)initWithFrame:(CGRect)frame utilityButtons:(NSArray *)utilityButtons paren self.utilityButtons = utilityButtons; self.utilityButtonWidth = [self calculateUtilityButtonWidth]; self.parentCell = parentCell; - self.utilityButtonSelector = utilityButtonSelector; // eh. + self.utilityButtonSelector = utilityButtonSelector; } return self; @@ -86,7 +87,7 @@ - (void)populateUtilityButtons { if (utilityButtonsCounter >= 1) utilityButtonXCord = _utilityButtonWidth * utilityButtonsCounter; [utilityButton setFrame:CGRectMake(utilityButtonXCord, 0, _utilityButtonWidth, CGRectGetHeight(self.bounds))]; [utilityButton setTag:utilityButtonsCounter]; - [utilityButton addTarget:self.parentCell action:self.utilityButtonSelector forControlEvents:UIControlEventTouchDown]; + [utilityButton addTarget:_parentCell action:_utilityButtonSelector forControlEvents:UIControlEventTouchDown]; [self addSubview: utilityButton]; utilityButtonsCounter++; } @@ -109,7 +110,8 @@ @interface SWTableViewCell () { @property (nonatomic, strong) SWUtilityButtonView *scrollViewButtonViewLeft; @property (nonatomic, strong) SWUtilityButtonView *scrollViewButtonViewRight; -@property (nonatomic, weak) UITableView *containingTableView; // Used for row height and selection +// Used for row height and selection +@property (nonatomic, weak) UITableView *containingTableView; @end @@ -124,6 +126,7 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus self.leftUtilityButtons = leftUtilityButtons; self.height = containingTableView.rowHeight; self.containingTableView = containingTableView; + self.highlighted = NO; [self initializer]; } @@ -211,17 +214,33 @@ - (void)initializer { - (void)scrollViewPressed:(id)sender { if(_cellState == kCellStateCenter) { - // Actually select the row + // Selection hack if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]){ NSIndexPath *cellIndexPath = [_containingTableView indexPathForCell:self]; [self.containingTableView.delegate tableView:_containingTableView didSelectRowAtIndexPath:cellIndexPath]; } + // Highlight hack + if (!self.highlighted) { + self.scrollViewButtonViewLeft.hidden = YES; + self.scrollViewButtonViewRight.hidden = YES; + NSTimer *endHighlightTimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(timerEndCellHighlight:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:endHighlightTimer forMode:NSRunLoopCommonModes]; + [self setHighlighted:YES]; + } } else { // Scroll back to center [self hideUtilityButtonsAnimated:YES]; } } +- (void)timerEndCellHighlight:(id)sender { + if (self.highlighted) { + self.scrollViewButtonViewLeft.hidden = NO; + self.scrollViewButtonViewRight.hidden = NO; + [self setHighlighted:NO]; + } +} + #pragma mark UITableViewCell overrides - (void)setBackgroundColor:(UIColor *)backgroundColor { @@ -362,6 +381,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView { @end + #pragma mark NSMutableArray class extension helper @implementation NSMutableArray (SWUtilityButtons) diff --git a/SWTableViewCell/ViewController.m b/SWTableViewCell/ViewController.m index 4c46897..f32b793 100644 --- a/SWTableViewCell/ViewController.m +++ b/SWTableViewCell/ViewController.m @@ -26,6 +26,7 @@ - (void)viewDidLoad self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.rowHeight = 90; + self.tableView.allowsSelection = NO; // We essentially implement our own selection self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); // Makes the horizontal row seperator stretch the entire length of the table view _testArray = [[NSMutableArray alloc] init]; @@ -96,6 +97,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N return cell; } +-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { + NSLog(@"scroll view did begin dragging"); +} + - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Set background color of cell here if you don't want white } @@ -137,7 +142,7 @@ - (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityBut NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell]; [_testArray removeObjectAtIndex:cellIndexPath.row]; - [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft]; break; } default: