Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

carlosbutler

macrumors 6502a
Original poster
Feb 24, 2008
691
2
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.

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

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors 6502a
Original poster
Feb 24, 2008
691
2
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors 6502a
Original poster
Feb 24, 2008
691
2
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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.