objective-cdeprecatedxcode4.2initwithstyle

How to fix: initWithFrame:CellFrame reuseIdentifier:cellIdentifier deprecated


I upgraded to XCODE 4.2 and suddenly i got all these warning signals. 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.

Could someone please give me a hint how i should replace the deprecated code with the initWithStyle and get the same result?

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;
}

Solution

  • 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;
        }