Reputation: 579
Hi i am working with the tableview.Now i am facing one issue. in my tableview when i use Long Gesture i need to display Alert View.it is working fine.when i click on buttonindex 0 in alert view i need to perform some Task.But in that i need indexpath. Below is my method for perform task
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(messageDeleteOrForword:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.tableView addGestureRecognizer:lpgr];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self deleteSpecificMessage];
}
if (buttonIndex==1) {
}
}
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
[alert show];
}
-(void)deleteSpecificMessage
{
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
}
but i need to pass some parameter to know the indexpath for that i use below line -(void)deleteSpecificMessage:(id)sender { } But how to call and assign parameter in alertview Please help me.
Upvotes: 1
Views: 272
Reputation: 1051
You can get indexpath using
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
You can check more details from this link
Also you can take a IndexPath
variable in .h file which can be assigned while selecting the tableview cell.
First of all Add your long tap gesture recognizer.
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(actionLongPressGeature:)];
[longPressGesture setDelegate:self];
[longPressGesture setMinimumPressDuration:0.3];
[tableView addGestureRecognizer:longPressGesture];
i.e Option 1
-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:tableViewJobs];
// You need to declare NSIndexPath *indexPathSelected in your .h File
indexPath = [tableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
// Perform your action i.e AlertView
}
}
Option 2:
-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer
{
// You need to declare NSIndexPath *indexPathSelected in your .h File
indexPath = [tableView indexPathForSelectedRow];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
// Perform your action i.e AlertView
}
}
and later on you can use this variable.
I hope this is you what you are looking for.
Upvotes: 0
Reputation: 2077
You can also use Objective-C Associated Objects for runtime properties. This features is available in <objc/runtime.h>
. For Example:
#import <objc/runtime.h>
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
objc_setAssociatedObject(alert, @"currentIndexPath", indexPath, OBJC_ASSOCIATION_RETAIN);
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSIndexPath *idxPath = objc_getAssociatedObject(alertView, @"currentIndexPath");
NSLog(@"%@",idxPath);
if (buttonIndex == 0) {
[self deleteSpecificMessage];
}
if (buttonIndex==1) {
}
}
Here are some links which is more helpful. http://kingscocoa.com/tutorials/associated-objects/ http://nshipster.com/associated-objects/
Using associate object you can attached multiple properties with any object and get easily. In first link you can also customize properties with defining Category.
Upvotes: 0
Reputation: 106
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else{
NSLog(@"selected row index %ld",(long)indexPath.row);
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
[alert show];
}
}
Upvotes: 0
Reputation: 27448
show alert view from didselectrowatindexpath
. set alertview tag as indexpath from this method. by that way you will integrate your alertview with indexpath. in alertview delegate you can got indexpath as it's tag. so you can use this tag to delete it or forward it as considering indexpath.
forexample :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
alert.tag = indexPath; //setting tag
[alert show];
}
Update (as ask in comment):
you can do something like this, here is an example,
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
here handleLongpress method
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil];
alert.tag = indexPath; //setting tag
[alert show];
}
}
hope this will help :)
Upvotes: 1
Reputation: 788
In your delete method you could add this:
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
Upvotes: 0