initWithFrame:reuseIdentifier:已过时。

30

在我的项目中,我收到了一个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个回答

60

请看这个苹果页面

这里的红色突出显示的函数和属性将在未来的 SDK 中被 Apple 删除。

因此,我们在创建应用程序时应该避免使用它们。

因为我们需要一个长期运行而不会崩溃的项目。

一个过时的方法意味着它已经被替换或归档,但在当前版本的语言中仍然有效。应该避免使用它,并可能导致问题/错误。请查看文档,其中应列出您可以使用的备选方法。

在这里,您应该使用该方法:

 - initWithStyle:reuseIdentifier: 

那么您的if循环将会是这样的

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

9

这个问题出现在《Beginning IOS 5 Development》一书中,该书由Mark、Nutting和La Marche合著。一些读者可能会通过该书来到这里,因为不推荐使用的代码出现在第265页。他们可能会认为问题出在他们自己身上!

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

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

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

注意,我已经放弃了autorelease,因为自动引用计数不喜欢它!
希望这可以帮到你。

1
使用这段代码:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                 reuseIdentifier:CellIdentifier] autorelease];

0

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

static NSString *SimpleTableIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接