I have custom UITableViewCell
s as well as headers and footers. Each of them has UITextField
s 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 UITableViewCell
s 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 UITableViewCell
s 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];
}
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.