Search code examples
iosuitableviewheaderuibuttonfooter

iOS - Make UITableView with custom cells editable on button click


I want to add a small button beside my UITableView header that when pressed changes my table view cells to be editable (setting a UITextField in place for the detail label) that I can edit and then I click the button again and it reverts and saves.

Or add a button to the footer that just reads Save and have the cells always editable.

How can I do this?


Solution

  • Subclass UITableViewCell, override layoutSubviews to prepare different presentation of normal and editing mode (@property(nonatomic, getter=isEditing) BOOL editing to decide which). Or you can override it's setEditing:animated, but never tried that.

    The button then should call something like

    [mytable setEditing:YES animated:YES];
    

    When you call this method with the value of editing set to YES, the table view goes into editing mode by calling setEditing:animated: on each visible UITableViewCell object. Calling this method with editing set to NO turns off editing mode. In editing mode, the cells of the table might show an insertion or deletion control on the left side of each cell and a reordering control on the right side, depending on how the cell is configured. (See UITableViewCell Class Reference for details.) The data source of the table view can selectively exclude cells from editing mode by implementing tableView:canEditRowAtIndexPath:.