I am trying to redraw my custom table cells when the device is rotated. So far I have what is below, and it sort of works. If I rotate the device, some change, and then I rotate the device again, and some more change, but it is not in any specific order and none are linked together.
So far I am only trying to push the labels further right when rotated (just to see if I can get it to work). I am doing [self.tableView reloadData]; when the device changes orientation.
So far I am only trying to push the labels further right when rotated (just to see if I can get it to work). I am doing [self.tableView reloadData]; when the device changes orientation.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSInteger LineNameTag = 2, StatusTag = 3;
UIInterfaceOrientation destinationOrientation = self.interfaceOrientation;
if (destinationOrientation == UIInterfaceOrientationPortrait || destinationOrientation == UIInterfaceOrientationPortraitUpsideDown) {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect textFrame;
textFrame.origin.x = 10;
textFrame.origin.y = 5;
textFrame.size.height = 15;
textFrame.size.width = 200;
UILabel *lineName = [[UILabel alloc] initWithFrame:textFrame];
lineName.tag = LineNameTag;
[cell.contentView addSubview:lineName];
[lineName release];
textFrame.origin.y += 18;
UILabel *status = [[UILabel alloc] initWithFrame:textFrame];
status.tag = StatusTag;
[cell.contentView addSubview:status];
[status release];
}
UILabel *lineName = (UILabel *) [cell.contentView viewWithTag:LineNameTag];
UILabel *status = (UILabel *) [cell.contentView viewWithTag:StatusTag];
lineName.text = [listOfLines objectAtIndex:indexPath.row];
status.text = [statusOfLines objectAtIndex:indexPath.row];
return cell;
}
else {
static NSString *CellIdentifier = @"CellLandscape";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect textFrame;
textFrame.origin.x = 50;
textFrame.origin.y = 5;
textFrame.size.height = 15;
textFrame.size.width = 200;
UILabel *lineName = [[UILabel alloc] initWithFrame:textFrame];
lineName.tag = LineNameTag;
[cell.contentView addSubview:lineName];
[lineName release];
textFrame.origin.y += 18;
UILabel *status = [[UILabel alloc] initWithFrame:textFrame];
status.tag = StatusTag;
[cell.contentView addSubview:status];
[status release];
}
UILabel *lineName = (UILabel *) [cell.contentView viewWithTag:LineNameTag];
UILabel *status = (UILabel *) [cell.contentView viewWithTag:StatusTag];
lineName.text = [listOfLines objectAtIndex:indexPath.row];
status.text = [statusOfLines objectAtIndex:indexPath.row];
return cell;
}
}