0

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;
}
PeterK
  • 4,243
  • 4
  • 44
  • 74

4 Answers4

6

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;
    }
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • I get: Class method '+dequeueReusableCellWithIdentifier:' not found (return type defaults to 'id') – PeterK Nov 08 '11 at 21:50
  • `dequeueReusableCellWithIdentifier` is UITableView method. – beryllium Nov 08 '11 at 21:52
  • would you please advice me how to fix that problem in your code above? – PeterK Nov 08 '11 at 22:18
  • Sorry, i was already sleeping when you asked a last question. This is very nice article with examples - [Subclassing UITableViewCell](http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW12). I advice to read a whole guide. – beryllium Nov 09 '11 at 07:21
1

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.

Rasmus Styrk
  • 1,296
  • 2
  • 20
  • 36
0
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
{
         cell  = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} 
return cell;
}
0

i just use this code cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

Alejandro Rangel
  • 1,670
  • 1
  • 20
  • 35