I have UITableViewController
with static grouped cells. In a particular view, I want to remove some of the cells. If I just hide them, it leaves an empty space, so I want to set the cell's rowHeight
to 0
instead. I'm having trouble doing this because I can't seem to get the indexPath of the cell I want to hide. I have a reference to it via IB connection. After reading around, it seems the best way to do this is via the heightForRowAtIndexPath
method. Here is my code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *myIndexPath = [self.tableView indexPathForRowAtPoint:self.billToPostalCell.center];
//crashes here
if ([indexPath isEqual:myIndexPath]) {
return 0;
}
return 44;
}
Before this, I was trying IndexPathForCell
, but also would crash. From what I read, heightForRowAtIndexPath
should work, even when the cells aren't visible. When it crashes, nothing shows up in the debugger either.
What you are doing is causing infinite recursion leading to a ... wait for it ... stack overflow!
Inside the heightForRowAtIndexPath:
method you can't ask the table for a cell or the indexPath for a row because those methods need to know the cell's height. So those call result in heightForRowAtIndexPath:
being called. And since you then call the same offending methods again, this goes on until things go boom.
Since your goal is to hide the cells, you should remove the cells from the table using the proper UITableView
method (deleteRowsAtIndexPaths:withRowAnimation:
). Of course you need to update your data model first (to remove the rows).