Uitableviewcell Without Using Deprecated Method Initwithframe:Reuseidentifier

UITableViewCell without using deprecated method initWithFrame:reuseIdentifier

just had to replace initWithFrame:reuseIdentifier: with the following.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
// you might want to add the UIView to [self contentView]
// so that in edit's the cell's content will be automatically adjusted.
ABTableViewCellView *myUIView = [[ABTableViewCellView alloc] initWithFrame:CGRectZero];

myUIView.opaque = YES;
contentViewForCell = myUIView;
[self addSubview:myUIView];
[myUIView release];
}

return self;
}

Also, apple has an example as Loren points out but they use initWithStyle:reuseIdentifier:

http://developer.apple.com/iphone/library/samplecode/TableViewSuite/Introduction/Intro.html

initWithFrame : reuseIdentifier : is deprecated

Take a look at this Apple's page

Here Red-Highlighted Functions and Properties will be Removed in Future by Apple in upcoming SDK.

so that we should avoid them while creating App.

Because we need longterm project which should run without crash.

a deprecated method means it has been replaced/retired but is still valid in current version of the language. it should be avoided and can cause problems/errors. check the documentation which should list an alternative method you can use.

Here you should use the method

 - initWithStyle:reuseIdentifier: 

Then your if loop would look like this

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}

initWithFrame:reuseIdentifier' is deprecated

The new initializer is using a UITableViewCellStryle instead of specifying the frame CGRect for the cell and you were just giving the frame to the superclass in [super initWithFrame:frame reuseIdentifier:reuseIdentifier]. So, there should be not problem to put all the same code in the new version, without the if statement.

You had :

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// all your stuff
}
return self;
}

You now have :

- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// all your stuff
}
return self;
}

How to fix: initWithFrame:CellFrame reuseIdentifier:cellIdentifier deprecated

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

What is alternative for initWithFrame:reuseIdentifier?

You must use: initWithStyle:reuseIdentifier:

Table Deprecations Question

Deprecated means that the method will soon be unavailable in the future sdk. You can always check what you should use instead by just finding this method in the docs.

initWithFrame:reuseIdentifier:

Initializes and returns a table cell
object. (Deprecated in iOS 3.0. Use initWithStyle:reuseIdentifier:
instead.)

This particular method was deprecated by

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

initWithFrame vs initWithStyle

Your implementation of cellForRowAtIndexPath should look similar to the following:

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";

CustomCell *cell = (CustomCell *)(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell.
cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
return cell;
}

where CustomCell is the name of your custom cell's class. Note that this implementation uses ARC (Automatic Reference Counting). If you don't happen to use this feature, add a autorelease call to the allocation of your cell.

CustomCell's initWithStyle implementation:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//do things
}
return self;
}

UITableViewCell not calling initWithFrame

initWithFrame is not called because you call

initWithFrame: reuseIdentifier:

replace

- (id) initWithFrame: (CGRect)frame {
self = [super initWithFrame: frame];

with

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithFrame: frame reuseIdentifier:reuseIdentifier];

Also please mind that initWithFrame:reuseIdentifier is a deprecated method and you should use initWithStyle:reuseIdentifier instead.



Related Topics



Leave a reply



Submit