Redrawing UITableViewCells on rotation

Go To StackoverFlow.com

2

I have custom UITableViewCells as well as headers and footers. Each of them has UITextFields in columns which line up. The positioning of these columns is determined on a percentage basis according to the width of the UITableView. (i.e. so it takes advantage of the wider screen when opened in landscape).

THe problem I have is when I rotate the view once the view controller has loaded. The UITableViewCells are still using the old positioning.

Having searched SO, I have now implemented the didRotateFromInterfaceOrientation: and reloaded data in this method.

This has now sorted the Headers and Footers. However, the UITableViewCells are still in their pre-rotated format. Is there a way to force the table to completely redraw all of its contents?

EDIT: My tableView:cellForIndexPath: code:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";
    UITextField *TextField1, *TextField2, *TextField3, *TextField4, *TextField5;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    float width = (self.tableView.frame.size.width - 68);

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

            TextField1 = [[UITextField alloc] initWithFrame:CGRectMake(((width*position1)+10.0), 10.0, (width*width1), 31.0)];
            TextField1.tag = 1;

            TextField2 = [[UITextField alloc] initWithFrame:CGRectMake((width*position2), 10.0, (width*width2), 31.0)];
            TextField2.tag = 2;

            TextField3 = [[UITextField alloc] initWithFrame:CGRectMake((width*position3), 10.0, (width*width3), 31.0)];
            TextField3.tag = 3;

                TextField4 = [[UITextField alloc] initWithFrame:CGRectMake((width*position4), 10.0, (width*width4), 31.0)];
            TextField4.tag = 4;

            TextField5 = [[UITextField alloc] initWithFrame:CGRectMake((width*position5), 10.0, ((width*width5)-40.0-editingWidth), 31.0)];
            TextField5.tag = 5;

            cell.accessoryType = UITableViewCellAccessoryNone;

            [cell.contentView addSubview:TextField1];
            [cell.contentView addSubview:TextField2];
            [cell.contentView addSubview:TextField3];
            [cell.contentView addSubview:TextField4];
            [cell.contentView addSubview:TextField5];

            [TextField1 release];
            [TextField2 release];
            [TextField3 release];
            [TextField4 release];
            [TextField5 release];

        } else {

            TextField1 = (UITextField *)[cell.contentView viewWithTag:1];
            TextField2 = (UITextField *)[cell.contentView viewWithTag:2];
            TextField3 = (UITextField *)[cell.contentView viewWithTag:3];
            TextField4 = (UITextField *)[cell.contentView viewWithTag:4];
            TextField5 = (UITextField *)[cell.contentView viewWithTag:5];

        }

    // Configure the cell...

    // Clear cell contents
    TextField1.text = @"";
    TextField2.text = @"";
    TextField3.text = @"";
    TextField4.text = @"";
    TextField5.text = @"";

    int sectionCount = 0;
    sectionCount = [workoutSectionsMutableArray count];

    // Repopulate cells

    NSMutableArray *cellDataArray = [self retrieveCellDataAtIndexPath:indexPath];

    TextField1.text = [[cellDataArray objectAtIndex:indexPath.row] 1];
    TextField2.text = [[cellDataArray objectAtIndex:indexPath.row] 2];
    TextField3.text = [[cellDataArray objectAtIndex:indexPath.row] 3];
    TextField4.text = [[cellDataArray objectAtIndex:indexPath.row] 4];
    TextField5.text = [[cellDataArray objectAtIndex:indexPath.row] 5];

    }

    return cell;

}

My didRotateFromInterfaceOrientation: method:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {

    [self.tableView reloadData];
}
2012-04-05 15:58
by Ben Thompson
Set a breakpoint in your "cell for index path", rotate the device, and see if (1) it is being called, and (2) it produces text fields of the proper width - dasblinkenlight 2012-04-05 16:03
Perhaps you could post the code where you do the repositioning of the columns, it seems like this should be a straightforwards issue to fix - Darren 2012-04-05 16:30
I have included the code as requested. As you can see, I setup the cells using calculations which work for either portrait or landscape orientations. But the command [reload Data] isn't enough to redraw cells, so the textFields maintain their existing layout, whereas the Headers and Footers get redrawn - Ben Thompson 2012-04-05 20:19


3

When you call reload on the table view, the tableView:cellForRowAtIndexPath: method is going to be called on your data source. Since you've already initialized the cells, the code within your if statement is not going to be called and the text fields will not be repositioned.

The easiest fix would be to simply move the text field code out of the if statement. I'm not sure if that would cause a noticeable performance hit or not.

By the way, your nameTextField, resistanceTextField etc. that you're adding to your cell are uninitialized, but I assume that's just a mixup that happened in transferring from your code to stackoverflow. I assume that it's supposed to be TextField1, TextField2 etc. that you're adding.

2012-04-05 20:44
by Darren
Yep - that did it. I removed the Frame definitions to outside of the if statement and this worked perfectly. Intuitively, I didn't think I could add a subview which didn't have key properties like size and position determined. Yes - the textField references weren't all transferred to SO properly. Updated now. Thank - Ben Thompson 2012-04-06 07:17
Ads