ios initWithFrame:reuseIdentifier:已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6967506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:59:39  来源:igfitidea点击:

initWithFrame : reuseIdentifier : is deprecated

iosxcode4deprecatedreuseidentifier

提问by a3116b

In my project i've got a Deprecations warning, initWithFrame : reuseIdentifier : is deprecated

在我的项目中,我收到了一个弃用警告,initWithFrame :reuseIdentifier :已弃用

I don't know what it mean, could some one tell me how to resolve this warning thanks

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

here is the short code

这是短代码

- (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;
}

and the warning is on that line :

并且警告在该行上:

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

回答by Vijay-Apple-Dev.blogspot.com

Take a look at this Apple's page

看看这个苹果的页面

Here Red-Highlighted Functions and Properties will be Removed in Future by Apple in upcoming SDK.

苹果将​​在未来的 SDK 中删除红色突出显示的函数和属性。

so that we should avoid them while creating App.

这样我们在创建应用程序时应该避免它们。

Because we need longterm project which should run without crash.

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

a deprecated method means it has been replaced/retired but is still valid in current version of the language. it should be avoided and can cause problems/errors. check the documentation which should list an alternative method you can use.

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

Here you should use the method

在这里你应该使用该方法

 - initWithStyle:reuseIdentifier: 

Then your if loop would look like this

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

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

回答by Tim

This problem appears in Beginning IOS 5 Development by Mark, Nutting and La Marche. Some readers may come here from that book where the deprecated code appears on page 265. They may assume the fault is theirs!

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

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

needs to be replaced by (as the contributors above point out)

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

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

Notice that I have dropped the autorelease as well because Auto Reference Counting does not like it!

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

Hope this helps.

希望这可以帮助。

回答by Yash Kaushik

Use this code:

使用此代码:

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

回答by Yash Kaushik

This should solve your problem:

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

static NSString *SimpleTableIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

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