Permalink
Browse files

Slightly changed product selection behavior

- Long press is now recognized before the finger is lifted (when the gesture
  begins, not when it ends)
- Tapping an already selected product always makes it the exclusive selection
  instead of deselecting it
- Long-pressing an already selected product deselects it
  • Loading branch information...
1 parent 064f725 commit 63ba103ea36558f1b6fc7877256f88ccb6a43067 Ole Zorn committed Jun 21, 2012
Showing with 41 additions and 34 deletions.
  1. +41 −34 Classes/DashboardViewController.m
@@ -332,45 +332,52 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
[cell.colorButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = [self accessoryViewForRowAtIndexPath:indexPath];
-
- bool t = [self.selectedProducts containsObject:product];
- if (t) {
- [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
- }
-
- UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleLongPress:)];
- [cell addGestureRecognizer:lpgr];
- [lpgr release];
-
+
+ if ([self.selectedProducts containsObject:product]) {
+ [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
+ }
+
+ UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
+ [cell addGestureRecognizer:longPressRecognizer];
+ [longPressRecognizer release];
+
return cell;
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
- if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
- DashboardAppCell * cell = ((DashboardAppCell*)gestureRecognizer.view);
- int i = [self.visibleProducts indexOfObject:cell.product];
- NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i+1 inSection:0];
- [self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
-
- if (cell.product) {
- if (self.selectedProducts) {
- if (![self.selectedProducts containsObject:cell.product]) {
- [self.selectedProducts addObject:cell.product];
- }
- } else {
- self.selectedProducts = [NSMutableArray arrayWithObject:cell.product];
- NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
- [self.productsTableView deselectRowAtIndexPath:indexPath animated:NO];
- }
- } else {
- self.selectedProducts = nil;
- [self deselectAllRowsInTableView:self.productsTableView exceptForIndexPath:nil];
- NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
- [self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
- }
- }
+ if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
+ DashboardAppCell * cell = ((DashboardAppCell*)gestureRecognizer.view);
+ NSUInteger i = [self.visibleProducts indexOfObject:cell.product];
+ NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i + 1 inSection:0];
+ [self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
+
+ if (cell.product) {
+ if (self.selectedProducts) {
+ if (![self.selectedProducts containsObject:cell.product]) {
+ [self.selectedProducts addObject:cell.product];
+ } else {
+ [self.selectedProducts removeObject:cell.product];
+ [self.productsTableView deselectRowAtIndexPath:indexPath animated:NO];
+ if (self.selectedProducts.count == 0) {
+ self.selectedProducts = nil;
+ [self deselectAllRowsInTableView:self.productsTableView exceptForIndexPath:nil];
+ NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
+ [self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
+ }
+ }
+ } else {
+ self.selectedProducts = [NSMutableArray arrayWithObject:cell.product];
+ NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
+ [self.productsTableView deselectRowAtIndexPath:indexPath animated:NO];
+ }
+ } else {
+ self.selectedProducts = nil;
+ [self deselectAllRowsInTableView:self.productsTableView exceptForIndexPath:nil];
+ NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
+ [self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
+ }
+ }
}
- (UIView *)accessoryViewForRowAtIndexPath:(NSIndexPath *)indexPath

0 comments on commit 63ba103

Please sign in to comment.