initWithFrame:reuseIdentifier:已弃用

时间:2011-08-06 14:35:29

标签: ios xcode4 deprecated reuseidentifier

在我的项目中,我有一个Deprecations警告,initWithFrame:reuseIdentifier:已弃用

我不知道这是什么意思,有人可以告诉我如何解决这个警告 感谢

这是短代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    NSString *cellValue = [itemsList objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    return cell;
}

并且警告就在那一行:

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

4 个答案:

答案 0 :(得分:60)

Take a look at this Apple's page

  

此处红色突出显示的功能和属性将在未来的SDK中由Apple删除。

以便我们在创建App时避免使用它们。

因为我们需要能够在没有崩溃的情况下运行的长期项目。

不推荐使用的方法意味着它已被替换/退役但在当前版本的语言中仍然有效。应该避免它,并可能导致问题/错误。查看应列出您可以使用的替代方法的文档。

在这里你应该使用方法

 - initWithStyle:reuseIdentifier: 

然后你的if循环看起来像这样

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

答案 1 :(得分:9)

这个问题出现在Mark,Nutting和La Marche的Beginning IOS 5 Development中。有些读者可能会从第265页出现不推荐代码的那本书来到这里。他们可能认为错误是他们的!

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: sectionsTableIdentifier] autorelease];

需要替换为(作为上述贡献者指出)

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: sectionsTableIdentifier];

请注意,我已经删除了自动释放,因为自动引用计数不喜欢它!

希望这有帮助。

答案 2 :(得分:1)

使用此代码:

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

答案 3 :(得分:0)

这可以解决您的问题:

static NSString *SimpleTableIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                   reuseIdentifier:SimpleTableIdentifier] autorelease];
}
相关问题