Reputation: 2413
I am using this code for cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == favouritesTable) {
cellValue = [licensePlateArray objectAtIndex:indexPath.row];
} else { // handle search results table view
cellValue = [filteredListItems objectAtIndex:indexPath.row];
}
static NSString *CellIdentifier = @"vlCell";
VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(@"Cell Created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[VehicleListCell class]]) {
cell = (VehicleListCell *)currentObject;
}
}
}
UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
pressRecongnizer.view.tag = indexPath.row;
//code added
[pressRecongnizer setValue:[NSNumber numberWithInt:indexPath.row] forKey:@"index"];
toDeleteObject = [results objectAtIndex:indexPath.row];
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];
// NSIndexPath *indexPathValue = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
// [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
// NSLog(@"indexPathValue: %@", indexPathValue);
cell.textLabel.font = [UIFont systemFontOfSize:10];
Favouritesdata *favdata = [results objectAtIndex:indexPath.row];
[[cell ignition] setImage:[UIImage imageNamed:@"ignition_flag.png"]];
[[cell direction] setImage:[UIImage imageNamed:@"south.png"]];
cell.licPlate.text = [favdata licenseplate];
NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);
return cell;
}
and in UILongPressGestureRecognizer code:
- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}
int index = [[recognizer valueForKey:@"index"] intValue];
NSLog(@"indexNew: %i", index);
VehicleListCell* cell = (VehicleListCell *)[recognizer view];
cellValueForLongPress = cell.licPlate.text;
NSLog(@"cell value: %@", cellValueForLongPress);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;
NSIndexPath *tableSelection = [favouritesTable indexPathForSelectedRow];
[favouritesTable selectRowAtIndexPath:tableSelection
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];
NSLog(@"NSIndexPath *tableSelection = %@", tableSelection );
alert.tag = recognizer.view.tag;
NSLog(@"alert.tag: %d", alert.tag);
[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Show on Map"];
[alert show];
}
In this code I have applied gesture recognizer, it has a dialog box which gives option to the user to delete the core data entry. Here the issue I am getting is that, I am unable to get the exact data entry value which is to be deleted, like it doesn't matter whichever cell/row is clicked the only core data entry will be deleted residing at index-0. I have spent many days to resolve this issue but not getting through.
Please guide me in this issue...
Thanks in advance
Upvotes: 0
Views: 350
Reputation: 2297
Check method names once, i did not checked them
-(void)tableCellPressed:(UILongPressGestureRecognizer*)gust{
UITableViewCell *cell = (id)gust.view;
NSIndexPath *ip = [tableView indexPathForCell:cell];
}
Upvotes: 1