【问题标题】:How to fix: initWithFrame:CellFrame reuseIdentifier:cellIdentifier deprecated如何修复:initWithFrame:CellFrame 重用标识符:cellIdentifier 已弃用
【发布时间】:2011-11-08 21:19:40
【问题描述】:

我升级到 XCODE 4.2,突然我收到了所有这些警告信号。大多数我能够修复,但以下我不知道如何修复。我已经尝试阅读它,但仍然有问题。

不推荐使用的代码是:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

我知道我需要使用以下内容:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

但是,我在测试时遇到了 CellFrame 等问题。

有人可以给我一个提示,我应该如何用 initWithStyle 替换已弃用的代码并获得相同的结果?

这里是完整的代码:

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(30, 33, 270, 25);
CGRect Label3Frame = CGRectMake(30, 56, 270, 25);
UILabel *lblTemp;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:16]];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];

//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 2;
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];

//Initialize Label with tag 3.
lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 3;
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];

return cell;
}

【问题讨论】:

  • 向我们展示您尝试使用 initWithStyle 的代码,并向我们展示您遇到的错误。

标签: objective-c deprecated xcode4.2 initwithstyle


【解决方案1】:

刚开始用样式初始化,然后设置reuired frame。我没有发现任何问题:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.frame = CellFrame;
    }

【讨论】:

  • 我得到:类方法 '+dequeueReusableCellWithIdentifier:' 未找到(返回类型默认为 'id')
  • dequeueReusableCellWithIdentifier 是 UITableView 方法。
  • 能否请您告诉我如何解决上述代码中的问题?
  • 对不起,当你问最后一个问题时,我已经在睡觉了。这是一篇非常不错的文章,带有示例 - Subclassing UITableViewCell。我建议阅读完整的指南。
【解决方案2】:

类似的东西

 cell = [[[UITableViewCell alloc] initWithStyle:somestyle reuseIdentifier:@"cellname"] autorelease]
 cell.frame = cellFrame;

我不确定你是否真的需要设置单元格框架,除非你想要一些特定的框架,它应该设置为 tableview 的任何内容。

【讨论】:

    【解决方案3】:

    我只是使用这个代码 cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    【讨论】:

      【解决方案4】:
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) 
      {
               cell  = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      } 
      return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-19
        • 1970-01-01
        • 2022-10-22
        • 2016-11-03
        • 2020-02-02
        • 1970-01-01
        • 2015-12-25
        相关资源
        最近更新 更多