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.