通用uitableview拉伸问题

发布于 2024-12-23 01:36:07 字数 3817 浏览 4 评论 0原文

我正在开发一个通用(iphone/ipad)应用程序。我在此应用程序中使用 uitableview 。使用代码绘制表格单元格。现在的问题是,如果我只在一次上输入代码,它可以通过单个代码安装在 iPad 或 iPhone 上。

为了实现这一点,我需要设置条件来检查设备,然后需要编码 2 次,即 1 次用于 iphone,另一次用于 iPad 来设置其背景图像和可用框架等。

对我来说,这看起来是垃圾和愚蠢的工作。有没有办法通过单一代码来实现这个目标。 我的代码如下。我正在使用 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;
}

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

I am developing an universal(iphone/ipad) application. I am using uitableview in this application. table cell being draw using code. Now problem is that it can be fit either on iPad or iPhone from single code, If I put code only on single time.

To achieve this I need to put condition to check the device and then need to code 2 time, mean 1 for iphone and another for iPad to set its background images and frames of uilable etc..

This is looking rubbish and stupid job to me. Is there any way to achieve this goal by single code.
My code is as below. I am using 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;
}

Please help and give me some ideas. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-12-30 01:36:07

您可以使用视图的frame 和bounds 属性动态决定框架,而不是在框架中传递硬编码的参数。但您必须照顾每个 UI 元素。

Instead of passing hardcoded agruments in frame, you can decide the frame dynamically using frame and bounds property of view. But you have to take care of each UI element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文