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 8011879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:09:00+00:00 2026-06-04T19:09:00+00:00

I am implementing a swipe to delete functionality in uitableview. I am able to

  • 0

I am implementing a swipe to delete functionality in uitableview. I am able to achieve that, but now i wish to display a alert message to confirm delete. I have created the tableview programmatically.

ViewController.h

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *tView;
    NSMutableArray *myArray;
    NSIndexPath *lastIndexPath;
}

@property(nonatomic,retain) UITableView *tView;
@property(nonatomic,retain) NSMutableArray *myArray;
@property(nonatomic,retain) NSIndexPath *lastIndexPath;

@end

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor=[UIColor lightGrayColor];

    myArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

    // Do any additional setup after loading the view, typically from a nib.
    tView = [[UITableView alloc] initWithFrame:CGRectMake(30, 30, 700, 800) style:UITableViewStylePlain];
    tView.delegate=self;
    tView.dataSource=self;
    [self.view addSubview:tView];
    [tView release];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [myArray count];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirm Delete" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];

    [alert show];
    [alert release];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        lastIndexPath = indexPath;
        NSLog(@"%@......%d",lastIndexPath,lastIndexPath.row);
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0)
    {
        NSLog(@"cancel");
    }
    else {
        NSLog(@"delete");
        [myArray removeObjectAtIndex:lastIndexPath.row];    //memory error 
        [tView deleteRowsAtIndexPaths:[NSArray arrayWithObject:lastIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //..do required thing and return cell
}

I get a memory error in the clickButtonAtIndex method for alert view. I think it is for the lastIndexPath, but the nslog() give the correct value in lastIndexPath.
What am i doing wrong?

  • 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-06-04T19:09:02+00:00Added an answer on June 4, 2026 at 7:09 pm

    I hate it when answers just say “You should’t do that”… So I’ll explain why you are getting an error and then hopefully appeal to your better judgement as to why you really ought not to do this (from a UI point of view).

    You are getting an error because you assign a value you do not own (you have not retained it) to an ivar directly. You probably meant to say self.lastIndexPath.

    What happens is that the alert view does not really appear until the next time through the run loop at which point indexPath has been autoreleased and you try to access it through a pointer.

    Changing lastIndexPath = indexPath; to self.lastIndexPath = indexPath; should solve the memory issue. (Since you marked the property as retain, assuming you synthesized it and did not write your own handler, when you use the self. prefix the synthesized accessor will retain it for you).

    (Incidentally this is why is not a bad idea to name your ivars differently from your properties (e.g. Apple uses <property_name>_, on my site I use m<Property_name> it makes it hard to make this sort of mistake).

    OK… But back to why you should not do this….

    You user has, at this point, made a recognizable gesture by swiping left and has then pressed a red button marked ‘delete’… And now you are going pop an alert and ask them to confirm? Really? OK, if you must…

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

Sidebar

Related Questions

I am trying to implement SMS kind of swipe and delete functionality. In case
In my code i m implementing swipe using the HorizontalScrollView for that i got
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
I am implementing a MyGestureDetector that extends a SimpleOnGestureListener. I borrowed the class from:
Possible Duplicate: UIScrollView. Any thoughts on implementing “infinite” scroll/zoom? I notice that in the
I want to swipe between activities, but I'm not sure what best practices would
I want to have a simple app that display the current date. Say for
Implementing a Thread by providing a new class that extends Thread and overriding its
Implementing a web service that uses Transport-level security with WCF over HTTP is pretty
i implementing Java WebService with JAX-WS and now need to secure my webservice 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.