0

I would like to have a UIActionSheet present itself when the user long presses on a UITableViewCell. The problem is, I don't know how to reference which cell was used to initiate the actionsheet. I know you can use tags in actionsheets to identify them, but I'm creating them for every cell dynamically, not as static sheets.

Here I create the long press recognizer and bind it to cell for every cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // other stuff

    UILongPressGestureRecognizer * tap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showRouteOptionsActionSheet:)];
    [cell addGestureRecognizer:tap];

    return cell;
}

And this method is called when you long press on the cell, this is where I create the ActionSheet

- (void)showRouteOptionsActionSheet:(UIGestureRecognizer *)gestureRecognizer{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
        UIActionSheet * routeOptions = [[UIActionSheet alloc] initWithTitle:@"Options" delegate:self cancelButtonTitle:@"Close" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Edit", @"More Information", nil];
        [routeOptions showInView:self.view];
    }
}

And this is called when you tap on a button in the ActionSheet

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0){
        // Delete
    } else if(buttonIndex == 1){
        // Edit
    } else if(buttonIndex == 2){
        // More Info
    }  else if(buttonIndex == 3){
        // Close
    }
}

In that last method, I have no way of knowing which cell the user pressed. As far as I am aware, you cannot pass variables with @selector's, but there must be a way about solving this.

There is a similar question on Stack Overflow here, but the solution there is to hard-code tags to the actionsheet. Because I create the actionsheet when the user long presses on the cell, that wouldn't work.

ecnepsnai
  • 1,882
  • 4
  • 28
  • 56
  • No, this is not a duplicate. In that question the OP is creating the ActionSheets statically, I am not. – ecnepsnai Aug 10 '14 at 00:15
  • 1
    In showRouteOptionsActionSheet, you can set the action sheet's tag to the row of the cell (see http://stackoverflow.com/questions/3924446/long-press-on-uitableview for how to get the indexPath from long press). If you need both row and section, a simple way is to use an ivar to "pass" the info. –  Aug 10 '14 at 00:25
  • It is a duplicate. Both questions need to deal with one delegate processing multiple action sheets. The solution is the same. – rmaddy Aug 10 '14 at 00:31
  • He wants to know which cell, not which action sheet was used `"I don't know how to reference which cell was used to initiate the action sheet"`. Create a variable called _selectedCell, and you can set it in the `showRouteOptionsActionSheet:(UIGestureRecognizer *)gestureRecognizer` method like this: `_selectedCell = (UITableViewCell *)gestureRecognizer.view;` Then, you can refer to _selectedCell from within your delegate method. If you need to keep track of which cell made which action sheet over time, create an NSMutableDictionary called _cellsForActionSheets. – michaelsnowden Aug 10 '14 at 00:36
  • I still completely disagree with rmaddy's vote, but he's clearly not going to listen to me. I'll try what you suggested Anna and doctordoder! – ecnepsnai Aug 10 '14 at 00:38
  • It's not that I'm not going to listen, It was simply a misunderstanding initially. I'm going to remove the duplicate closure. However, using tags is still a viable solution. – rmaddy Aug 10 '14 at 01:08
  • Either set the action sheet's tag to the cell's row or use an ivar to save a reference to the cell. Then reference the tag or the cell ivar in the action sheet delegate. – rmaddy Aug 10 '14 at 01:14

0 Answers0