简体   繁体   English

如何修复:不建议使用initWithFrame:CellFrame复用标识符:cellIdentifier

[英]How to fix: initWithFrame:CellFrame reuseIdentifier:cellIdentifier deprecated

I upgraded to XCODE 4.2 and suddenly i got all these warning signals. 我升级到XCODE 4.2,突然得到了所有这些警告信号。 Most of the i am able to fix but the following I do not know how to. 大多数我都可以解决,但以下我不知道如何解决。 I have tried to read-up on it but still have problem. 我已经尝试过阅读它,但是仍然有问题。

The code that is deprecated is: 不推荐使用的代码为:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

I know that i need to use the following: 我知道我需要使用以下内容:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

However, i get problem with the CellFrame etc. when i test. 但是,我在测试时遇到了CellFrame等问题。

Could someone please give me a hint how i should replace the deprecated code with the initWithStyle and get the same result? 有人可以给我一个提示,我应该如何用initWithStyle替换不推荐使用的代码并获得相同的结果吗?

Here is the full code: 这是完整的代码:

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(30, 33, 270, 25);
CGRect Label3Frame = CGRectMake(30, 56, 270, 25);
UILabel *lblTemp;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:16]];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];

//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 2;
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];

//Initialize Label with tag 3.
lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 3;
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];

return cell;
}

Just at first initialize with style, and after set reuired frame. 刚开始时使用样式进行初始化,然后设置所需的框架。 I don't see any problems: 我没有发现任何问题:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.frame = CellFrame;
    }

Something along those lines 遵循这些原则

 cell = [[[UITableViewCell alloc] initWithStyle:somestyle reuseIdentifier:@"cellname"] autorelease]
 cell.frame = cellFrame;

Iam not sure if you really need to set the cell frame unless you want some specific frame, it should be set to whatever the tableview is. 我不确定是否确实需要设置单元格框架,除非您需要某些特定的框架,应将其设置为任何tableview。

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
{
         cell  = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} 
return cell;
}

我只是使用此代码cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM