Search code examples
iosuitableviewxcode4.2uitoolbar

How to edit the UITableViewCell using UIButton?


I am new to iPhone programming, and stuck in a situation.

The situation is that I want UITableView to enter the editing mode on UIButton click. Upon entering edit mode there I should have a check box on each cell. And I wanted to change the checked cells images if I click UIToolBarButton.

How can I achieve this. Any help would be appreciated.


Solution

  • Just like lnafziger said to enter the editing mode call

    [self.tableview setEditing:YES animated:YES];
    

    in the function that is connected to your button.

    To show checkboxes on your cells when the table is in editing mode add these lines to viewDidLoad :

    [self.tableView setAllowsSelectionDuringEditing:YES];
    [self.tableView setAllowsMultipleSelectionDuringEditing:YES];
    

    Then for your UIBarButtonItem to change the images add a for loop with the [self.tableView indexPathsForSelectedRows] array, and change each cells image like so:

    NSArray *paths = [self.tableView indexPathsForSelectedRows];
    for (NSIndexPath *path in paths) {
        UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:path];
        //change cell.imageView's image
    }