【问题标题】:Setter method not working for a property of a UITableViewCell SubclassSetter 方法不适用于 UITableViewCell 子类的属性
【发布时间】:2014-01-22 21:33:11
【问题描述】:

我正在尝试获取一个表格来显示名称和等级列表。在每个单元格中,我希望有 2 个 UILabel 作为子视图——一个 UILabel 显示排名,一个 UILabel 显示名称。

我创建了一个 UITableViewCell 子类,它有如下两个属性:

@interface MyTableViewCell : UITableViewCell

@property UILabel* rankLabel;
@property UILabel* nameLabel;

除了每个属性的@synthesize 之外,我没有在 MyTableView 的 .m 实现文件中添加任何内容。实现文件的其余部分正是 xCode 在您创建 UITableViewCell 子类时为您提供的内容。

然后,在包含该表的 masterViewController 中,我有以下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    Person *object = _objects[indexPath.row];

    if (cell == nil) {
    cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.height, 0, cell.frame.size.width - cell.frame.size.height, cell.frame.size.height)];
    cell.nameLabel.text = object.name;
    [cell addSubview:cell.nameLabel];

    NSString* rankString = [object.rank stringValue];
    cell.rankLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.height, cell.frame.size.height)];
    cell.rankLabel.text = rankString;
    [cell addSubview:cell.rankLabel];
}

当我在调试中运行它时,它会到达我设置 cell.nameLabel 并分配它的第一行,并引发以下错误:

[UITableViewCell setNameLabel:]: unrecognized selector sent to instance 0x108fa64c0

真的不明白为什么 setNameLabel 方法在这里应该有问题。有任何想法吗?提前致谢。

【问题讨论】:

  • 您根本不使用情节提要吗?或者只是不适用于这个特定的单元格?

标签: ios objective-c uitableview


【解决方案1】:

该错误似乎告诉您确切的问题。它没有说 [MyTableViewCell setNameLabel:] 它说 [UITableViewCell setNameLabel:] 你可能忘记将故事板中单元格的类更改为 MyTableViewCell...

编辑:不,他没有使用故事板,但崩溃显然与单元格属于错误的类有关。

编辑:是的,他是,或者至少 Xcode 是为他做的。

【讨论】:

  • 我没有为这个程序使用故事板,但感谢你提醒我注意这个差异......我现在回顾代码,看看我是否能弄清楚为什么会有这种差异在那里。
  • 干杯!应该注意到您是在代码中执行此操作的。这似乎很奇怪,因为您清楚地设置了标识符...
  • 很好,你是对的。我以编程方式做所有事情,所以我认为我根本不需要处理情节提要,但是 XCode 默认为您提供的基本情节提要需要像您所说的那样将单元格类型更改为 MyTableViewCell 。谢谢!
  • 太棒了! “我的一个朋友”曾经犯过一两次同样的错误。 ;-)
猜你喜欢
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2014-10-06
相关资源
最近更新 更多