【问题标题】:Resolving compile-time error regarding custom cells解决有关自定义单元格的编译时错误
【发布时间】:2013-01-30 04:24:11
【问题描述】:

我正在使用 iOS 6 编写应用程序。 这是 ViewController.m 文件中的 sn-p 代码:

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

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        cell = _customCell;
        _customCell = nil;

    }

    cell.firstName.textLabel = @"dsdsds";
    cell.middleName.textLabel = @"N/A";
    cell.lastName.textLabel = @"daasdsdasa";

    return cell;
}

这些代码行给了我错误(Property 'firstName' not found on object of type 'CustomCell*'):

cell.firstName.textLabel = @"dsdsds";
        cell.middleName.textLabel = @"N/A";
        cell.lastName.textLabel = @"daasdsdasa";

CustomeCell.h:

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *firstName;

@property (strong, nonatomic) IBOutlet UILabel *middleName;
@property (strong, nonatomic) IBOutlet UILabel *lastName;

+(NSString*) reuseIdentifier;
@end

In the Outlets of CustomCell.xib:
firstName -> label
middleName -> label
lastName -> label

Referencing Outlets:
customCell -> File's Owner

Selecting the firstName label:
Referencing Outlets:
firstName -> CustomCell
firstName -> CustomCell -CustomCellIdentifier

Selecting the middleName label:
lastName -> Custom Cell - CustomCellIdentifier
middleName -> Custom Cell

Selecting the lastName label:
lastName -> Custom Cell
middleName -> Custom Cell- Custom Cell Identifier

那么,问题是什么?在我看来,这与奥特莱斯有关。

【问题讨论】:

  • 你在.m类中合成了这些属性吗
  • 如果iOS6不需要综合属性
  • 事实上我无法在 hat 文件中合成它们,因为首先它必须在 .h 文件中声明。虽然现在它在不同的文件中
  • 什么是文件的所有者类,该类的 customCell 属性是 CustomCell 还是 UITableViewCell ?
  • #import "CustomCell.h"了吗?

标签: ios objective-c viewcontroller iboutlet outlet


【解决方案1】:

错误(Property 'firstName' not found on object of type 'CustomCell*'): 表示编译器不知道名称为firstName 的属性。您需要通知编译器类中可用的属性,通常是通过导入头文件。

因此,在您的表格视图代码所在的文件顶部,输入:

#import "CustomCell.h"

(请注意,它位于您的 @implementation 块之前,与其他 #import 相同。

【讨论】:

  • 已导入。如您所见,没有影响。
  • 真的吗?在 ViewController.m 的顶部?这是您遇到的唯一错误吗?
  • 糟糕,中间名和姓氏还有两个错误
  • 您可以将整个 ViewController.m 复制并粘贴到您的问题中吗?
【解决方案2】:

我在您的代码中看到了几个错误:

cell.firstName.textLabel = @"dsdsds";
cell.middleName.textLabel = @"N/A";
cell.lastName.textLabel = @"daasdsdasa";

问题是 UILabel 没有像 textLabel 这样的属性。

改成:

cell.firstName.text  = @"dsdsds";
cell.middleName.text = @"N/A";
cell.lastName.text   = @"daasdsdasa";

如果您没有合成该属性,请改用以下内容:

cell._firstName.text  = @"dsdsds";
cell._middleName.text = @"N/A";
cell._lastName.text   = @"daasdsdasa";

也改变:

if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
}

if (cell == nil) {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = (CustomCell *)[nib objectAtIndex:0];
}

【讨论】:

  • 根据 XCODE 文本已被弃用,我将使用 textLabel。 resy 修复也无济于事。
  • @uml: 用于单元格。不推荐使用 Cell.text,因此您需要使用 cell.textLabel。 UILabel 没有像 textLabel 这样的属性
  • @uml:这是一个错字。请检查一下
  • 不。同样的错误。我听从了你关于命名等的建议
  • @uml:你改成 _firstName 了吗?
【解决方案3】:

为什么你的 .h 文件中有 +reuseIdentifier 类方法?您继承自的 UITableViewCell 类有一个具有该名称的属性,所以这有点令人困惑。

此外,惯例是为您的出口使用“弱”:@property (weak, nonatomic) IBOutlet UILabel *firstName;,因为该视图将具有其子视图的强副本。

我不确定为什么找不到您的财产。这是一个运行时错误。如果没有正确连接笔尖;一旦您尝试实例化该视图,您就会得到一个异常。您是否将该视图的“类”设置为 CustomCell? (虽然如果您不这样做,您应该将常规 TableViewCell 出列并且无法设置您的插座,所以...)嗯。

【讨论】:

  • 弱在 ARC 和 GC 中是不允许的。切换到弱并执行合成时收到错误。 .xib 的类已设置为 CustomCell
  • iOS 没有 GC;和强/弱是专门针对ARC的。如果您的目标是较旧的 iOS;您有时可以使用 unsafe_unretained 代替弱。无论如何,祝你的问题好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多