使用JSQMessagesViewController创建自定义单元格 - Objective C

时间:2016-11-15 14:38:09

标签: ios objective-c xcode jsqmessagesviewcontroller

您好我正在使用JSQMessagesViewController库,我正在尝试在头像图片下方添加一个视图。我已经阅读了常见问题解答和文档,并根据instructions完成了所有工作,但它似乎仍然没有用。你能帮帮我吗?

我创建了3个自定义类,类的子类:

  • JS​​QMessagesCollectionViewCell
  • JSQMessagesCollectionViewCellIncoming
  • JS​​QMessagesCollectionViewCellOutgoing

ChatCell,JSQMessagesCollectionViewCell的底层

@interface QChatCell : JSQMessagesCollectionViewCell

@end
@implementation QChatCell

@end

ChatCellIncoming,ChatCell的子类。我为此创建了一个xib文件(QChatCellIncoming.xib),它是JSQMessagesCollectionViewCellIncoming xib文件的副本:

@interface QChatCellIncoming : QChatCell

@end

@implementation QChatCellIncoming

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.messageBubbleTopLabel setTextAlignment:NSTextAlignmentLeft];
    [self.cellBottomLabel setTextAlignment:NSTextAlignmentLeft];
}

+ (UINib *)nib {
    return [UINib nibWithNibName:@"QChatCellIncoming" bundle:nil];
}

+ (NSString *)cellReuseIdentifier {
    return @"QChatCellIncoming";
}
@end

ChatCellOutgoing,ChatCell的子类。我已为此创建了一个xib文件(QChatCellOutgoing.xib),它是JSQMessagesCollectionViewCellOutgoing xib文件的副本:

@interface QChatCellOutgoing : QChatCell

@end

@implementation QChatCellOutgoing

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.messageBubbleTopLabel setTextAlignment:NSTextAlignmentRight];
    [self.cellBottomLabel setTextAlignment:NSTextAlignmentRight];
}

+ (UINib *)nib {
    return [UINib nibWithNibName:@"QChatCellOutgoing" bundle:nil];
}

+ (NSString *)cellReuseIdentifier {
    return @"QChatCellOutgoing";
}
@end

在视图中的聊天控制器类中加载我执行此操作:

self.outgoingCellIdentifier = [QChatCellOutgoing cellReuseIdentifier];
self.outgoingMediaCellIdentifier = [QChatCellOutgoing mediaCellReuseIdentifier];

[self.collectionView registerNib:[QChatCellOutgoing nib] forCellWithReuseIdentifier:self.outgoingCellIdentifier];
[self.collectionView registerNib:[QChatCellOutgoing nib] forCellWithReuseIdentifier:self.outgoingMediaCellIdentifier];

self.incomingCellIdentifier = [QChatCellIncoming cellReuseIdentifier];
self.incomingMediaCellIdentifier = [QChatCellIncoming mediaCellReuseIdentifier];

[self.collectionView registerNib:[QChatCellIncoming nib] forCellWithReuseIdentifier:self.incomingCellIdentifier];
[self.collectionView registerNib:[QChatCellIncoming nib] forCellWithReuseIdentifier:self.

incomingMediaCellIdentifier];

在cellForItemAtIndexPath函数中,我完成了这个:

- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSQMessagesCollectionViewCell *cell;

    if ([self incoming:messages[indexPath.item]]) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.incomingCellIdentifier forIndexPath:indexPath];
        [cell.textView setTextColor:COLOR_CHAT_INCOMING_TEXT];
        return cell;
    }
    else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.outgoingCellIdentifier  forIndexPath:indexPath];
        [cell.textView setTextColor:COLOR_CHAT_OUTGOING_TEXT];
        return cell;
    } 
}

当我进入聊天屏幕时,应用程序崩溃时出现以下错误:

Unknown class _TtC5Q_Fix17QChatCellOutgoing in Interface Builder file.
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UICollectionViewCell 0x7f821cd42a80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key avatarContainerView.'

2 个答案:

答案 0 :(得分:0)

您的问题的根源在故事板中。某些东西指向名为_TtC5Q_Fix17QChatCellOutgoing的非现有类。

无需手动点击故事板中的所有对象:

  • 在您选择的文本编辑器中打开storyBoardName.storyboard文件
  • 搜索TtC5Q_Fix17QChatCellOutgoing
  • 如果没有结果,请搜索avatarContainerView

您希望找到类似的内容:

`customClass="TtC5Q_Fix17QChatCellOutgoing"`

在一个嵌套的结构中,应该给你一个很好的提示,在哪里挖掘故事板。

答案 1 :(得分:0)

实际问题是以ChatCell的ChatCellIncoming,子类为根源的probaby。我已经为此创建了一个xib文件(QChatCellIncoming.xib),它是JSQMessagesCollectionViewCellIncoming xib文件的副本:通过这样做即(复制文件)您可能错过了更改类。我以前多次犯过这个错误,错误信息很容易引起误解。
因此,如果您打开Xcode并打开QChatCellIncoming.xib,请在界面构建器中选择单元格本身

interface builder

然后看右侧面板IE(Utilities Panel)选择&#34;身份检查器&#34;看起来像这样:屏幕截图

identity Inspector

并从JSQMessagesCollectionViewCellIncoming

更改您的类

enter image description here

到您的班级名称QChatCellIncoming现在您的UI知道哪些代码支持它并且不会混淆。然后清理并运行它应该解决这个问题。如果您有更多问题,请告诉我。