30

在我的项目中,我收到了弃用警告, 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

4 回答 4

60

看看这个Apple的页面

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

所以我们应该在创建应用程序时避免它们。

因为我们需要长期项目,该项目应该运行而不会崩溃。

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

在这里你应该使用方法

 - initWithStyle:reuseIdentifier: 

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

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier] autorelease];
}
于 2011-08-06T14:40:04.823 回答
9

此问题出现在 Mark、Nutting 和 La Marche 的《开始 IOS 5 开发》中。一些读者可能从第 265 页上出现不推荐使用的代码的那本书来到这里。他们可能会认为是他们的错!

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

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

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

请注意,我也放弃了自动释放,因为自动引用计数不喜欢它!

希望这可以帮助。

于 2012-08-27T14:43:10.643 回答
1

使用此代码:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                 reuseIdentifier:CellIdentifier] autorelease];
于 2012-06-06T09:31:04.737 回答
0

这应该可以解决您的问题:

static NSString *SimpleTableIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                   reuseIdentifier:SimpleTableIdentifier] autorelease];
}
于 2012-07-19T06:55:41.410 回答