initWithFrame:reuseIdentifier:已弃用

a31*_*16b 30 deprecated reuseidentifier ios xcode4

在我的项目中,我有一个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;
}
Run Code Online (Sandbox Code Playgroud)

警告就在那条线上:

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
Run Code Online (Sandbox Code Playgroud)

Vij*_*com 60

看看这个Apple的页面

Apple将在即将发布的SDK中删除未来的红色突出显示的功能和属性.

所以我们应该在创建App时避免使用它们.

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

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

在这里你应该使用这个方法

 - initWithStyle:reuseIdentifier: 
Run Code Online (Sandbox Code Playgroud)

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

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier] autorelease];
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答,感谢您的链接,非常有趣和有用 (2认同)

Tim*_*Tim 9

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

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: sectionsTableIdentifier] autorelease];
Run Code Online (Sandbox Code Playgroud)

需要被替换为(正如上面指出的贡献者)

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: sectionsTableIdentifier];
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助.