Hassam
Hassam

Reputation: 253

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.

Upvotes: 3

Views: 895

Answers (2)

lnafziger
lnafziger

Reputation: 25740

To enter edit mode using a button, call the following code from the button's action method:

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

To change the checkBox, you need to set the editingAccessoryView of the cell to your custom view (typically in cellForRowAtIndexPath).

Upvotes: 1

JonasG
JonasG

Reputation: 9324

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
}

Upvotes: 5

Related Questions