How can I change UITableViewCell using UIButton?

I am new to iPhone programming and am stuck with a situation.

The situation is that I want the UITableView to enter edit mode on a click of the UIButton. When entering edit mode, I should have a checkbox in each cell. And I wanted to change the images of checked cells if I click the UIToolBarButton.

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

+3


source to share


2 answers


Like lnafziger said to enter edit mode

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

      

in a function connected to your button.

To show checkboxes in cells when the table is in edit mode, add these lines to viewDidLoad

:



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

      

Then to make your UIBarButtonItem change the images, add a for loop with an array [self.tableView indexPathsForSelectedRows]

and change the image of each cell like this:

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

      

+5


source


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

cells to a custom view (usually in cellForRowAtIndexPath

).

+1


source







All Articles