Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 23, 2011, 10:02 AM   #1
carlosbutler
macrumors 6502a
 
carlosbutler's Avatar
 
Join Date: Feb 2008
Location: London City
Customviews and rotating them

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;
	}
}
__________________
24" iMac, 3GHz, 15" MacBook Pro quad
iPod Classic, iPod Nano, Apple TV
iPhone since release of original
easy-bills.co.uk
carlosbutler is offline   0 Reply With Quote
Old Jan 23, 2011, 11:56 AM   #2
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 23, 2011, 12:42 PM   #3
carlosbutler
Thread Starter
macrumors 6502a
 
carlosbutler's Avatar
 
Join Date: Feb 2008
Location: London City
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?
__________________
24" iMac, 3GHz, 15" MacBook Pro quad
iPod Classic, iPod Nano, Apple TV
iPhone since release of original
easy-bills.co.uk
carlosbutler is offline   0 Reply With Quote
Old Jan 23, 2011, 02:19 PM   #4
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Quote:
Would it be anything to do with the way that I am calling things, or how I am reloading the data?
Dunno.

Quote:
being called in the willAnimateRotationToInterfaceOrientation:duration method. Is this correct?
I would try didRotateFromInterfaceOrientation:
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 24, 2011, 10:53 AM   #5
carlosbutler
Thread Starter
macrumors 6502a
 
carlosbutler's Avatar
 
Join Date: Feb 2008
Location: London City
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!
__________________
24" iMac, 3GHz, 15" MacBook Pro quad
iPod Classic, iPod Nano, Apple TV
iPhone since release of original
easy-bills.co.uk
carlosbutler is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
rotation, uitableviewcell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
General: Music video NOT rotating... pr0230 iOS 7 2 Jan 14, 2014 06:52 AM
Calendar Rotating Schedule FightForTheUser MacBook Pro 0 Sep 2, 2013 10:33 PM
iPhone: Anyway to have rotating contact pics ValerieDurden Jailbreaks and iOS Hacks 0 Feb 12, 2013 12:14 AM
iOS 6 screen not rotating McGiord iOS 6 6 Nov 3, 2012 09:31 PM
Slim Rotating Case harpangel36 iPad Accessories 1 Aug 14, 2012 08:30 PM

Forum Jump

All times are GMT -5. The time now is 10:24 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC