开发者

What is alternative for initWithFrame:reuseIdentifier?

Can you help me fix this as the method initWithFrame:reuseIdentifier is deprecated:

static NSString *CellIdentifier = @”Cell”;

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}

Thx in a开发者_高级运维dvance,

Stephane


You must use: initWithStyle:reuseIdentifier:


a better approach is to define a getter in your CustomCell.m

-(NSString*) reuseIdentifier
{
    return @"Cell";
}

and in that method you return your reuse identifier. Then you can alternatively create yourself a setter for a variable as well thereby allowing you to have any set of custom cells with whatever reuse identifier you plan to use.

another method that includes the reuse identifier and is not depreciated is

[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];


Apart from the other answers, here's one thing you're doing which sticks out as not right:

cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

Calling [CustomCell alloc] implies you have subclassed UITableViewCell and are using your own init method that you coded in there (and I know this is not the case by reading the rest of your question).

You might possibly mean to change that line to:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleWhatever reuseIdentifier:CellIdentifier] autorelease];

You don't need to use initWithFrame as the cell's frame is decided by the width of the UITableView it's a part of and the height you pass in heightForRowAtIndexPath in the UITableView's data source methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜