Mashhadi
Mashhadi

Reputation: 3024

How to edit a TableView in iPhone?

In my application i need an edit button for my tableview that can delete a row or can change its position. it is really easy when i am using a default navigation bar but now in my case i am using a custom bar that is infect an imageview & now i need a button that can edit a tableview. i am not using the default navigation bar.

so plz help me

Upvotes: 0

Views: 3281

Answers (4)

Sourabh P Shroff
Sourabh P Shroff

Reputation: 88

YOU CAN USE SWIPE TO DELETE FEATURE. THIS CAN BE DOEN IN FOLLOWING WAY.

you need to implement following 3 delegate method of tableView.

tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath: 

In method

tableView:editingStyleForRowAtIndexPath:

RETURN TYPE SHOULD BE

UITableViewCellEditingStyleDelete.

Upvotes: 0

Dave Anderson
Dave Anderson

Reputation: 12274

You just need to set the editing property of the UITableView, in your UITableViewController implement something like;

- (void) editingButtonPressed:(id)sender {
  if([self isEditing]) {
    [sender setText:@"Edit" forState:UIControlStateNormal];
    [self setEditing:NO animated:YES];
  } else {
    [sender setText:@"Done" forState:UIControlStateNormal];
    [self setEditing:YES animated:YES];
  }
}

And hook that up to your button or image and replace the setText with setImage if you have no text.

Here is my init method for a UITableViewController using a custom UIToolbar which adds two buttons to the navigation bar in place of the right navigation bar button.

- (id) init {

    [super initWithStyle:UITableViewStyleGrouped];

    UIBarButtonItem *email = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Email.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(composeEmail:)] autorelease];
    UIBarButtonItem *bookmark = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBookmark:)] autorelease];
    [bookmark setStyle:UIBarButtonItemStyleBordered];

    CustomToolbar *buttonToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 93, 45)];
    [buttonToolbar setBarStyle:UIBarStyleBlackTranslucent];
    [buttonToolbar setItems:[NSArray arrayWithObjects:email, bookmark, nil] animated:NO];

    [[self navigationItem] setTitle:@"Table with Custom Toolbar"];
    [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:buttonToolbar] autorelease]];

    [buttonToolbar release];

    return self;
}

When creating the buttons I use action:@selector(customMethodName) when creating the buttons to hook up my methods to the button actions in this case composeEmail and addBookmark which load the new views for those tasks.

Upvotes: 1

Amit Vaghela
Amit Vaghela

Reputation: 2970

You have to add UIButton instance to that imageview and implement a method that will execute when an event occurs on that UIButton.

Code for the method will look like,

if(tableView.editing)
[tableView setEditing:NO animated:YES]
else
[tableView setEditing:YES animated:YES]

Read UITableView Documentation for more info.

Upvotes: 0

Gypsa
Gypsa

Reputation: 11314

you can add a button on your imageview and on that button click set your tableviewediting to yes.

here's the code how to add a imageview and button:-

UIImageView *imageView2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    [imageView2 setImage:[UIImage imageNamed:@"bottom bar_gda.png"]];
    [self.view addSubview:imageView2];
    [self.view bringSubviewToFront:imageView2];
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setFrame:CGRectMake(280, 3, 26, 36)];
    deleteButton.contentMode = UIViewContentModeScaleAspectFill;     
    UIImage *newImage12 = [UIImage imageNamed:@"check.png"];
    [deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
    [deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];
    [deleteButton addTarget:self action:@selector(editmethod:) forControlEvents:UIControlEventTouchUpInside];
    [imageView2 addSubview:deleteButton]; 

Upvotes: 0

Related Questions