ios – 这个类不再是密钥的密钥值编码

我的项目中有一个实体的详细信息屏幕(实际上是’照片’),可以有评论.如果是,则屏幕显示最近的3和“查看全部”按钮.问题是如何显示3条评论. (在 Android上我只是使用LinearLayout.)TableView不适合,因为我可以看到(由于有静态高度),所以我决定创建我的自定义模板/部分视图CommentView并将其添加到scrollView为每个注释.

我用视图和一些子控件(用户照片,用户名,日期,文本)创建了XIB文件.另外,我创建了从UIView提供的类CommentView.文件的所有者设置为CommentView.顶级视图的类也设置为CommentView(我尝试了各种组合).我从顶级视图(vContent)和所有子视图创建了插座.

这是CommentView.h:

@interface CommentView : UIView

@property (strong,nonatomic) IBOutlet CommentView *vContent;
@property (weak,nonatomic) IBOutlet UIImageView *ivUserPhoto;
@property (weak,nonatomic) IBOutlet UILabel *lUserName;
@property (weak,nonatomic) IBOutlet UILabel *lCreated;
@property (weak,nonatomic) IBOutlet UILabel *lText;


@end

这是CommentView.m:

#import "CommentView.h"

@implementation CommentView

-(void)awakeFromNib {
    [[NSBundle mainBundle] loadNibNamed:@"CommentView" owner:self options:nil];
    [self addSubview: self.vContent];
}

@end

然后我尝试在实体的视图中添加注释,并从问题标题中获得该错误.这不是我第一次得到它,但现在我真的不知道问题是什么……

此行发生错误(我从PhotoViewController调用它):

CommentView *commentView = [[[NSBundle mainBundle] loadNibNamed:@"CommentView" owner:self options:nil]objectAtIndex:0];

全文:
‘[setValue:forUndefinedKey:]:此类与密钥ivUserPhoto不符合密钥值编码.

我非常感谢你的帮助!谢谢!

解决方法

我在这里找到了另一个例子.所以我像这样重写了我的CommentView.m ……
@implementation CommentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        NSArray *nibContents = [[NSBundle mainBundle]loadNibNamed:@"CommentView" owner:self options:nil];
        [self addSubview:nibContents[0]];
    }

    return self;
}

@end

…以及我添加这样的子视图的方式:

CommentView *commentView = [[CommentView alloc]initWithFrame:CGRectMake(0,320,81)];

commentView.lUserName.text = comment.user.name;
[self.vComments addSubview:commentView];

现在它按照我的预期工作.

我仍然不完全理解为什么有很多例子在我的问题中以及为什么它对我不起作用…我希望我的代码可以节省时间给某人…

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...