Customviews and rotating them

Discussion in 'iPhone/iPad Programming' started by carlosbutler, Jan 23, 2011.

  1. macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #1
    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;
    	}
    }
    
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     
  3. thread starter macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #3
    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?
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Dunno.

    I would try didRotateFromInterfaceOrientation:
     
  5. thread starter macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #5
    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:
     

Share This Page