什么是initWithFrame的替代方法:reuseIdentifier?

时间:2011-09-06 23:45:11

标签: iphone ios ios4 uitableview

你可以帮我解决这个问题吗:initWithFrame方法:不推荐使用reuseIdentifier:

static NSString *CellIdentifier = @”Cell”;

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

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

}

提前,

的Stephane

3 个答案:

答案 0 :(得分:5)

您必须使用:initWithStyle:reuseIdentifier:

答案 1 :(得分:3)

更好的方法是在CustomCell.m中定义一个getter

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

并在该方法中返回重用标识符。 然后你也可以自己创建一个变量的setter,从而允许你拥有任何一组自定义单元格以及你计划使用的任何重用标识符。

包含重用标识符且未折旧的另一种方法是

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

答案 2 :(得分:0)

除了其他答案之外,你正在做的一件事情就是不正确:

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

调用[CustomCell alloc]意味着您已经使用了UITableViewCell的子类,并且正在使用您在其中编码的自己的init方法(我知道通过阅读其余问题并非如此)。

您可能意味着将该行更改为:

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

您不需要使用initWithFrame,因为单元格的框架由UITableView的宽度决定,它是UITableView数据源方法中heightForRowAtIndexPath传递的高度。