Search code examples
iosuitableviewcore-datansfetchedresultscontroller

Swift: Add empty element in table view (Core Data)


I want to add a 'new element' cell to my UITableView so that after text typing and tapping 'Enter' the new entry must be created. Something like here but only in the end of the list. How can I do this? In my UITableViewController I use NSFetchedResultsController that handles operations with Core Data.


Solution

  • You will need to amend the tableView delegate and datasource methods to include the extra row:

    1. Add another prototype cell to your storyboard to represent your "extra" row. Design it accordingly (subclassing if necessary) and assign it a different cell identifier.
    2. Amend numberOfRowsInSection to return the FRC sectionInfo count plus one
    3. Amend cellForRowAtIndexPath to dequeue with the different cell identifier if indexPath.row is equal to the FRC sectionInfo count (ie. the last row). Configure the cell accordingly.
    4. Amend didSelectRowAtIndexPath to trigger the code to add the new entry.
    5. Check/amend all other delegate/datasource methods (eg. editingStyleForRowAtIndexPath) to ensure they check the indexPath.row and handle the extra row appropriately.