I am new to iPad development. Here, I have a tableview with dynamic content. Each cell contains two labels, second label may increase or decrease. Finally I am adding the labels to cell. But when I rotate the simulator the labels in cell are not changing. I cannot set the frame for label. Please help me.
Thanks in advance.
UILabel *label1 = nil;
UILabel *label2 = nil;
NSLog(@"it decides the tablecell");
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
label1 =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 44) ];
//label1 = [[UILabel alloc] initWithFrame:CGRectZero];
[label1 setLineBreakMode:UILineBreakModeWordWrap];
[label1 setMinimumFontSize:FONT_SIZE];
[label1 setNumberOfLines:0];
[label1 setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
label2 = [[UILabel alloc] initWithFrame:CGRectZero];
[label2 setMinimumFontSize:FONT_SIZE];
[label2 setNumberOfLines:0];
[label2 setLineBreakMode:UILineBreakModeWordWrap];
[label2 setFont:[UIFont systemFontOfSize:FONT_SIZE]];
}
NSString *text1 = [arrOutline1 objectAtIndex:[indexPath row]];
NSString *text2 = [arrOutline2 objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake((CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2))/ 2, 20000.0f);
CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label1 setText:text1];
[label2 setText:text2];
[label1 setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN-10,( CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2))/2, MAX(size1.height, 44.0f))];
NSLog(@"now it is in cell methd");
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight)
{
[label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,500 , MAX(size2.height, 44.0f))];
NSLog(@"orientation is :LAndscape");
//[[cell contentView] addSubview:label2];
[cell addSubview:label2];
}
else
{
NSLog(@"orientation is Portrait.");
[label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,200 , MAX(size2.height, 44.0f))];
[cell addSubview:label2];
}
//[label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,( CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2)) /2 , MAX(size2.height, 44.0f))];
cellHeight =size2.height + 44;
label1.backgroundColor =[UIColor clearColor];
lab开发者_如何学编程el2.backgroundColor =[UIColor clearColor];
[[cell contentView] addSubview:label1];
[label1 release];
[label2 release];
return cell;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[TableOutline reloadData];
return YES;
}
You need to resize your subviews automatically when the iPad rotates?
1) Make sure that your parent view (inb your case I guess the table view cell) has autoresizesSubviews
set to YES (This will be the default)
2) Make sure that the labels you add have the correct value set for the autoresizingMask
e.g.
// To keep label1 the same size and have label2 stretch to fill the rest of the cell . . .
[label1 setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
[label2 setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
Here, you have set label 1 to be a fixed distance from the left hand side and be a fixed width. You have set label two to be a fixed distance from the left and right but have a variable width. When your table cell's frame changes, it will automatically change the frames of the labels inside it.
精彩评论