Can I show / hide a specific cell in a UITableView depending on the state of another cell?

I have a UITableView with the "Grouped" style, which I use to set some parameters in my application. I would like one of the cells of this UITableView to be displayed only depending on whether the other of these UITableView cells is activated or not. If this is not the case, the first cell should be displayed (preferably with smooth animation), if it is, the first cell should be hidden.

I tried returning nil to the corresponding -tableView:cellForRowAtIndexPath: to hide the cell, but this does not work and throws an exception instead.

I'm currently stuck and not in the know how to solve this, so I hope some of you can point me in the right direction.

+6
objective-c iphone cocoa-touch
source share
4 answers

Here is a handy post in which the author provides some source code for performing animations in the currently selected cell:

http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html

It uses this in the context of NSFetchedResultsController, but you can see how it uses various calls to add / remove cells and sections.

Now, in your case, you will need to modify any array that you use to place the data used to create the rows in the View table when you β€œactivate” your cell, and then selectively use:

  • Tableview: insertRowsAtIndexPaths: withRowAnimation:
  • Tableview: deleteRowsAtIndexPaths: withRowAnimation:
  • Tableview: insertSections: withRowAnimation:
  • Tableview: deleteSections: withRowAnimation:

to set it up correctly (you can start with tableView: reloadData :, but this is inefficient).

I understand that the API may be a little more complicated, but take the time to read it and understand what the various calls make. Understanding how the UITableView uses its data source and delegate, as well as the chain of events that occur when cells are selected / deleted / etc., are Important if you want to get everything in order (and without crashing).

+9
source share

You must remove data from hidden cells from the table data source.

For example, if you use an array when an action occurs that causes the cell to hide, you remove the object for this row from the array. Then, as the data source of the table view, the array will return one smaller total and will return only valid cells for each row in this account (no nil).

This approach may require supporting a second array with all objects (including hidden ones).

To refresh the view, see reloadRowsAtIndexPaths: withRowAnimation:.

+9
source share
 [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:withRowAnimation:]; // or insertRowsAtIndexPaths:withAnimation: [tableView endUpdates]; 
+2
source share

Before calling cellForRowAtIndexPath, numberOfRowsInSection is called. You must return the corresponding cell value in this section, so if you want to show only 1 cell, return it. The logic of which cells are shown should be partially implemented in both methods.

0
source share

All Articles