Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:05:19+00:00 2026-05-28T19:05:19+00:00

In cellForRowAtIndexPath: method I have implemented gesture recognizer for long press, -(UITableViewCell *)tableView:(UITableView *)tableView

  • 0

In cellForRowAtIndexPath: method I have implemented gesture recognizer for long press,

-(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:)];

toDeleteObject = [results objectAtIndex:indexPath.row];
pressRecongnizer.view.tag = indexPath.row;

pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];

cell.textLabel.font = [UIFont systemFontOfSize:10];

Favouritesdata *favdata = [results objectAtIndex:indexPath.row];

[[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
[[cell direction] setImage:[UIImage imageNamed:@"south.png"]];

cell.licPlate.text = [favdata licenseplate];

NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);

return cell;}

And in tableCellPressed

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state != UIGestureRecognizerStateBegan) {
    return;
}

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] ;

alert.tag = recognizer.view.tag;

[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Show on Map"];

[alert show];}

And in alertView:

-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alert buttonTitleAtIndex:buttonIndex];

if([title isEqualToString:@"Remove from Favourites"])
{
    NSLog(@"cellValueForLongPress: %@", cellValueForLongPress);

    [results removeObjectAtIndex:alert.tag];

    [context deleteObject:toDeleteObject];

    NSLog(@"alert.tag:::: %d", alert.tag);   

}
else if([title isEqualToString:@"Show on Map"])
{
    NSLog(@"Go to MapView");

    Maps *detailViewFromLabel = [Maps alloc];

    [self.view addSubview:detailViewFromLabel.view];

}

NSError *error;

if (![context save:&error]) {
    NSLog(@"Error Occured");
}

[favouritesTable reloadData];}

Through this code entry from the core data gets deleted but the issue is that it only deletes the entry at index:0 and not the entry which is selected from the table.

How can I resolve this issue ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:05:20+00:00Added an answer on May 28, 2026 at 7:05 pm

    You’re doing it backwards anyway, but here is your problem:

    pressRecongnizer.view.tag = indexPath.row; // view is nil at this point 
    pressRecongnizer.minimumPressDuration = 0.5f; 
    [cell addGestureRecognizer:pressRecongnizer]; // now we have a view!
    [pressRecongnizer release];
    

    You set the tag on the view of pressRecogniser before you set the view itself. So you are setting a tag to nothing, ending up with zero as the tag and this is the row you keep deleting.

    Add a gesture recogniser to your cell when it is first created (if cell==nil) then in your action method get hold of the index path of the cell (UITableView has a method for this, indexPathForCell:), and use that to determine which element you have selected and which element should be deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; in this method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; but I
I have the following Objective-c Function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger
I was calling UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; from -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath in
this code works fine until I start scrolling: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType
I have the following code: - (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath{ TestCell* cell
This is the code of my cellForRowAtIndexPath of my UITableView - (UITableViewCell *)tableView:(UITableView *)tableView
I have a UITableViewCell that is being instantiated (in the cellForRowAtIndexPath method) with [cell
I have used custom cell class for my uitableview and used in cellForRowAtIndexPath method.
I have a UITableView with UITableViewCells that are filled in the cellForRowAtIndexPath method with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.