views:

36

answers:

2

I have a UILabel in a custom UITableViewCell that gets resized when the device is rotated. The text in this label needs to be recalculated after the rotation because I am cutting it down to size and appending some text at the end.

E.g. the datamodel has: "This is a run-on sentence that needs to stop."

In portrait mode it becomes "This is a run-on sent... more"

In landscape mode it becomes "This is a run-on sentence that... more"

From (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation I am able to access the visible UITableViewCells and update the descriptions.

The problem seems to be that there are UITableViewCells that are cached but I can't get to. When I scroll the UITableView after a rotation, one or two cells that are below the visible area after the rotation don't have the correct text in the label. So they haven't been rendered via (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - but they weren't returned by [tableView visibleCells] (or via looping through all views returned via [tableView subViews]).

I've tried to access the "extra" cells via this method:

for (int index=max + 1; index < max + 3 && index < [cellTypes count]; index++) {
    NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:index inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:updatedPath];
    if (cell == nil) { continue; }
    [self updateCellForRotate:cell forRow:index];
}

(where max is the biggest row returned from visibleCells) but cell is always nil.

Is there anyway to flush the cache of UITableViewCells so that they don't get re-used? Or to access them so I can update them?

Thanks!

A: 

When you create the cell in cellForRowAtIndexPath:, add it to an array. Then, loop through the array, updating the text as necessary.

Hope this helps,
jrtc27

EDIT:

You say they are custom cells - could you not update your text in your UITableViewCell subclass?

jrtc27
Argh... I totally thought this would work. Doesn't seem to. I'm wondering if it hasn't rotated the cells at this point (I'm doing the update in the table view controller "didRotateFrom...") Note that it DOES update the visible cells just like my previous method did. I'm thinking it is must be the cells not rotating until they are being displayed.
Michael Kernahan
Well, I haven't solved it yet, but I used your array idea to print out all of the cells in the debugger... it appears that the cells that have an incorrect length label have the wrong dimensions.
Michael Kernahan
A: 

Firstly, to reload all of your table cells use [self.tableView reloadData]

Secondly, add the line of code that is responsible for the shrinking inside the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

Example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Some identifier and recycling stuff

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        //Make labels smaller
    }
    else {
        //Make them bigger
    }
}

Or you can just call your updateCellForRotate:forRow: method when making them. But I'm not sure how that function works, so I can't be too specific.

MishieMoo
Surely recreating all of the cells on orientation change can cause a performance dip? I could be wrong though (it has been known) ;)
jrtc27
Yeah, "cellForRowAtIndexPath" doesn't seem to do it. Nor did reloadData - should have posted that in my original. Will investigate updateCellForRotate - they're custom cells from xibs though.
Michael Kernahan
If you're recycling your cells (which is the best way to manage memory on a table!) reloading data is what you want to do, as that does "refresh the cache" in your own terms. If you're not recycling cells, this is a much bigger beast, so you would have to manually redraw each one.
MishieMoo
I am recycling the cells. [tableview reloadData] does seem to update all the visible cells great, but I am still having the problem show up with one or two cells when I scroll down.
Michael Kernahan
I've just changed my implementation to [self.tableView reloadData] which gets me to exactly where I was in only one line. Still have the problem until the user scrolls to the cell, away and then back again. Hopefully they don't notice. :-S Thanks for the help!
Michael Kernahan