Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a Static Cell from a UITableView designed in StoryBoard

The solution is probably very simple, but I couldn't just find it .. !

Working with storyboard (iOS 5), I have a tableViewController, and a designed STATIC tableview with 5 sections, with differents static cell inside each section.

My question is: How to delete a cell programatically in the viewWillAppear?

For example, I have a cell designed for a date

IBOutlet UITableViewCell * cellForDate; 

And.. if there's not date, I want to remove my cell.

cellForDate.hidden = true; //Hide the cell, but leave a blank space 

Ive tried [tableView deleteRowsAtIndexPaths...] didn't work

Anyone got an idea?

like image 791
Marc Avatar asked Nov 24 '11 20:11

Marc


People also ask

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

What is prototype cell?

A prototype cell acts a template for your cell's appearance. It includes the views you want to display and their arrangement within the content area of the cell. At runtime, the table's data source object creates actual cells from the prototypes and configures them with your app's data.


2 Answers

Try to hide the cell before it is shown, in UITableViewDelegate's tableView:willDisplayCell:forRowAtIndexPath: method. That's the final method where you can manipulate the cell's appearance. This however won't remove the space the cell should take, so another thing you can try is to set cell row's height to 0 using the tableView:heightForRowAtIndexPath: method of the same protocol.

Another method, which is most robust, is to devise a method for determining whether you need the date cell in the section and depending on the result, return the proper number of rows in the section, and return other section row cells taking the situation into account. And then reload tableView's data on viewWillAppear.

like image 129
macbirdie Avatar answered Sep 25 '22 03:09

macbirdie


Hopefully this is a bit more of a universal solution, but it's still not perfect

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];     if (cell.hidden) {         return 0;     } else {         return [super tableView:tableView heightForRowAtIndexPath:indexPath];     } } 

With this method you set your IBOutlet bound cell to be hidden, and then when the table tries to display a row, it first checks to see if the cell (using super call to get the cell) should be hidden, if so it returns a height of 0 which effectively removes it from the list. If the cell shouldn't be hidden then it gets the height from the super so whatever would normally happen still happens.

The only issue with this is that start and end cells are responsible for the divider at the top and bottom of the list or group. So if you hide the last cell in a group the bottom divider on the group will be inset slightly like it is on normal rows rather than full width. This is only a problem if you're hiding the top or bottom rows AND you are using a divider AND you care about totally standard display. The best solution in my case was simply to not hide the top or bottom rows. I moved any content that might be hidden to the middle of the group. Alternatively you could just accept that it isn't quite standard, or disable row dividers on your table.

like image 39
ima747 Avatar answered Sep 24 '22 03:09

ima747