在类方法之外获取集合视图的indexPath(JSQMessagesViewController)

时间:2017-05-27 18:25:16

标签: ios swift jsqmessagesviewcontroller

我尝试为聊天气泡设置自定义颜色,具体取决于发送邮件的人的性别。对于传出的气泡这样做很容易,我只是使用switch语句并检查我的性别字典中的当前用户的值。这很好,因为传出的气泡将始终是相同的颜色。

然而,对于传入的泡泡来说,它更加困难,因为它是一个群聊,并且颜色/性别将根据发送消息的人而有所不同。

我认为我需要的是获取消息的索引路径,比如在类方法(例如头像函数)中,我可以说

let message = messages[indexPath.row]

然后根据它改变颜色,例如

private func setupIncomingBubble() -> JSQMessagesBubbleImage {

    // How to get indexPath to use in here????
    let message = messages[indexPath.row]
    let gender = genderDictionary.value(forKey: message.senderId)

    switch gender {
    case "male":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "2573C5"))
    case "female":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "E452CE"))
    default:
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "848484"))
    }
}

但是,我不确定如何在类方法之外获取此索引路径。是否可能,或者我需要采取单独的方法?

编辑:

private func setupIncomingBubble() -> JSQMessagesBubbleImage {

    let cell: JSQMessagesCollectionViewCell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
    let message: JSQMessage = self.messages[indexPath.item]

    let gender = genderDictionary.value(forKey: message.senderId)

    switch gender {
    case "male":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "2573C5"))
    case "female":
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "E452CE"))
    default:
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor(hexString: "848484"))
    }

}

let celllet message行的错误,"使用未解析的标识符indexPath"

1 个答案:

答案 0 :(得分:0)

使用超级关键字&的索引路径它将继承indexpath

CollectionView
 let cell: JSQMessagesCollectionViewCell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
 let msg: JSQMessage = self.messages[indexPath.item]