私はユニバーサル(iphone / ipad)アプリケーションを開発しています。このアプリケーションではuitableviewを使用しています。コードを使用して描画されているテーブルセル。今の問題は、コードを1回だけ置くと、iPadまたはiPhoneのどちらにも1つのコードで収まるということです。
これを実現するには、デバイスをチェックするための条件を設定してから、2回コーディングする必要があります。つまり、iPhoneの場合は1回、iPadの場合は1回、背景画像や使用可能なフレームなどを設定します。
これは私にはごみと愚かな仕事に見えます。単一のコードでこの目標を達成する方法はありますか?私のコードは以下の通りです。xcode4.2ARCを使用しています
- (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;
}
助けて、私にいくつかのアイデアを教えてください。前もって感謝します。