2011-12-26 59 views
0

我正在开发一个通用(iphone/ipad)应用程序。我在这个应用程序中使用uitableview。表格单元正在使用代码绘制。现在的问题是,它可以适合iPad或iPhone从单个代码,如果我只在一次性代码。universal uitableview拉伸问题

为了实现这一点我需要把状态检查设备,然后需要2时间进行编码,平均1 iphone,另一个用于iPad来设置其背景图像和uilable等帧..

这是寻找垃圾和愚蠢的工作给我。有没有什么办法通过单一代码来实现这个目标。 我的代码如下。我正在使用xcode 4.2 ARC

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIFont *fontName=[UIFont fontWithName:@"HelveticaNeue LT 57 Cn" size:18.0f]; 
    UIFont *fontTitle=[UIFont fontWithName:@"HelveticaNeue LT 57 Cn" size:12.0f]; 
    UIFont *grayLight=[UIFont systemFontOfSize:12.0f]; 
    //UIFont *fontBold=[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; 

    UITableViewCell *cell = [[UITableViewCell alloc] init]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    NSMutableDictionary *dic=[arrEmails objectAtIndex:indexPath.row]; 
    //NSLog(@"%@",dic); 
    int tot=4; 
    NSString *imageName=[NSString stringWithFormat:@"inbox_list%d",(indexPath.row%tot)+1]; 

    UIImageView *imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(-30, 10, 303, 70)]; 
    [imgBg setImage:[UIImage imageNamed:imageName]]; 
    [cell.contentView addSubview:imgBg]; 
    NSString *name=nameDisplayType==1 ? [[dic valueForKey:@"firstName"] stringByAppendingFormat:@" %@", [dic valueForKey:@"lastName"]] : [[dic valueForKey:@"lastName"] stringByAppendingFormat:@" %@", [dic valueForKey:@"firstName"]]; 

    UILabel *lblFirstName=[[UILabel alloc] initWithFrame:CGRectMake(30 ,15,150,20)]; 
    [lblFirstName setText:name]; 
    [lblFirstName setBackgroundColor:[UIColor clearColor]]; 
    [lblFirstName setTextColor:UIColorFromRedGreenBlue(162, 23, 25)]; 
    [lblFirstName setTextAlignment:UITextAlignmentLeft]; 
    [lblFirstName setFont:fontName]; 
    [lblFirstName setBackgroundColor:[UIColor clearColor]]; 
    [cell.contentView addSubview:lblFirstName]; 


    UILabel *lblEmailId=[[UILabel alloc] initWithFrame:CGRectMake(30,28,200,20)]; 
    [lblEmailId setText:[dic valueForKey:@"emailId"]]; 
    [lblEmailId setBackgroundColor:[UIColor clearColor]]; 
    [lblEmailId setTextAlignment:UITextAlignmentLeft]; 
    [lblEmailId setFont:grayLight]; 
    [lblEmailId setTextColor:[UIColor grayColor]]; 
    [lblEmailId setBackgroundColor:[UIColor clearColor]]; 
    [cell.contentView addSubview:lblEmailId]; 




    NSDateFormatter *formatter=[[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"yyyy-MM-dd"]; 
    NSDate *date=[formatter dateFromString:[dic valueForKey:@"date"]]; 
    [formatter setDateFormat:@"dd MMM, yyyy"]; 
    NSString *formattedDate=[formatter stringFromDate:date]; 
    //NSLog(@"%@ formated:%@",[dic valueForKey:@"date"],formattedDate); 

    UILabel *lblDate=[[UILabel alloc] initWithFrame:CGRectMake(190,15,100,20)]; 
    [lblDate setText:formattedDate]; 
    [lblDate setBackgroundColor:[UIColor clearColor]]; 
    [lblDate setTextAlignment:UITextAlignmentLeft]; 
    [lblDate setFont:grayLight]; 
    [lblDate setTextColor:[UIColor blackColor]]; 
    [cell.contentView addSubview:lblDate]; 


    UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(20,53,200,20)]; 
    [lblTitle setText:[dic valueForKey:@"title"]]; 
    [lblTitle setBackgroundColor:[UIColor clearColor]]; 
    [lblTitle setTextAlignment:UITextAlignmentLeft]; 
    [lblTitle setFont:fontTitle]; 
    [lblTitle setTextColor:[UIColor blackColor]]; 
    [cell.contentView addSubview:lblTitle]; 

    UIButton *btnViewGift=[[UIButton alloc] initWithFrame:CGRectMake(235, 55, 20, 20)]; 
    [btnViewGift addTarget:self action:@selector(btnViewGiftPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnViewGift setTag:indexPath.row]; 
    [cell.contentView addSubview:btnViewGift]; 


    cell.accessoryType = UITableViewCellAccessoryNone; 


    return cell; 
} 

请帮忙给我一些想法。提前致谢。

回答

0

您可以使用帧和边界属性来动态决定帧,而不是在帧中传递硬编码的基准。但是你必须照顾每个UI元素。

+1

Like try -UILabel * lblFirstName = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.origin.x + someOffset,self.view.frame.origin.y + someOtherOffset,self.view.frame.size .width/someWidthOffset,self.view.frame.size.height/someHeighOffset)]; - >它会决定框架。 – rishi 2011-12-26 14:51:56