Programming iOS 8: Dive Deep into Views, View Controllers, and Frameworks by Neuburg Matt

Programming iOS 8: Dive Deep into Views, View Controllers, and Frameworks by Neuburg Matt

Author:Neuburg, Matt [Neuburg, Matt]
Language: eng
Format: azw3, epub, mobi
ISBN: 9781491908730
Publisher: O'Reilly Media
Published: 2014-11-23T16:00:00+00:00


Deleting Cells

Deletion of cells is the default, so there’s not much for us to do in order to implement it. If our view controller is a UITableViewController and we’ve displayed the Edit button in a navigation bar, everything happens automatically: when the user taps the Edit button, the view controller’s setEditing:animated: is called, the table view’s setEditing:animated: is called, and the cells all show the Minus button at the left end. The user can then tap a Minus button; a Delete button is shown at the cell’s right end. You can customize the Delete button’s title with the table view delegate method tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:.

What is not automatic is the actual response to the Delete button. For that, we need to implement tableView:commitEditingStyle:forRowAtIndexPath:. Typically, you’ll remove the corresponding entry from the underlying model data, and you’ll call deleteRowsAtIndexPaths:withRowAnimation: or deleteSections:withRowAnimation: to update the appearance of the table. As I said a moment ago, you must delete the row or section in such a way as to keep the table display coordinated with the model’s structure. Otherwise, the app may crash (with an extremely helpful error message).

To illustrate, let’s suppose once again that the underlying model is a pair of parallel arrays of strings (self.sectionNames) and arrays (self.sectionData). Our approach will be in two stages:

Deal with the model data. We’ll delete the datum for the requested row; if this empties the section array, we’ll also delete that section array and the corresponding section name.

Deal with the table’s appearance. If we deleted the section array, we’ll call deleteSections:withRowAnimation: (and reload the section index if there is one); otherwise, we’ll call deleteRowsAtIndexPaths:withRowAnimation::



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.