Skip to content

Commit

Permalink
Slightly changed product selection behavior
Browse files Browse the repository at this point in the history
- 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
Ole Zorn committed Jun 21, 2012
1 parent 064f725 commit 63ba103
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions Classes/DashboardViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 63ba103

Please sign in to comment.