Question

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.

Was it helpful?

Solution

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).

OTHER TIPS

You shouldn't be changing the cells visibility and height. If you want to remove the cells and add them back later, or never add them back you should be using the insert and delete methods on the UITableView. Here is a good starting point documentation.

Additionally, you shouldn't use methods on the tableView from within tableView:heightForRowAtIndexPath: like indexPathForRowAtPoint: as the tableView has to work out its own height before it can calculate things like this. You are probably getting an error because you are in a infinite loop.

Use -deleteRowsAtIndexPaths:withRowAnimation to delete rows. Never manipulate the visibility of a cell as a substitute for removing it; that can lead to serious memory issues and is just general bad practice. Just pass in an NSArray filled with the NSIndexPath of the row you'd like to remove.

You mentioned that the indexPathForRowAtPoint: method wasn't working. This is almost surely a problem with the point you're passing in to it. Be sure to log out the value of billToPostalCell.center to be sure you're passing in a legitimate value. However, there really isn't a need to do this if you're dealing with static cells, as you should already know the index path (at least for the initial cell removal).

As others have mentioned, the documentation on removing cells provides all the additional information you should need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top