PDA

View Full Version : Customviews and rotating them




carlosbutler
Jan 23, 2011, 09:02 AM
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.

- (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;
}
}



PhoneyDeveloper
Jan 23, 2011, 10:56 AM
The usual way to do this is to create a UITableViewCell subclass and override layoutSubviews. In layoutSubviews the code adjusts the frames based on the orientation.

I'm not sure why the code you show doesn't work. Stepping through it should make this clear though.

carlosbutler
Jan 23, 2011, 11:42 AM
Thanks for the response. Would you mind explaining how I would go about doing that? I'm a bit of a nOOb when it comes to iOS programming, literally just started :P

Would it be anything to do with the way that I am calling things, or how I am reloading the data?

The [self.tableView reloadData]; is being called in the willAnimateRotationToInterfaceOrientation:duration method. Is this correct?

PhoneyDeveloper
Jan 23, 2011, 01:19 PM
Would it be anything to do with the way that I am calling things, or how I am reloading the data?

Dunno.

being called in the willAnimateRotationToInterfaceOrientation:duration method. Is this correct?

I would try didRotateFromInterfaceOrientation:

carlosbutler
Jan 24, 2011, 09:53 AM
Well not that many people read this topic... but CLEAN ALL TARGETS

I loaded the Xcode project on another Mac of mine, and it worked! I completely forgot to clean all targets and build! :rolleyes: