initWithFrame: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];
看看这个苹果的页面
这里,红色突出显示的函数和属性将在未来的SDK中由Apple删除。
所以我们应该在创buildApp时避开它们。
因为我们需要长期运行而不会崩溃的项目。
弃用的方法意味着它已被replace/退役,但在当前版本的语言中仍然有效。 应该避免,并可能导致问题/错误。 检查应该列出你可以使用的替代方法的文档。
在这里你应该使用该方法
- initWithStyle:reuseIdentifier:
那么你的if循环看起来像这样
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }
这个问题出现在Mark,Nutting和La Marche开始的IOS 5开发中。 有些读者可能会从第265页的那本书中find这些代码。他们可能会认为是他们的错!
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: sectionsTableIdentifier] autorelease];
需要被(如上面的贡献者所指出的)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: sectionsTableIdentifier];
请注意,我也放弃了autorelease,因为自动引用计数不喜欢它!
希望这可以帮助。
使用此代码:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
这应该可以解决你的问题:
static NSString *SimpleTableIdentifier; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease]; }