首页 > 解决方案 > 如何在ios中的JSQMessagesViewController中的Incoming Buble下方添加时间

问题描述

*我正在使用 JSQMessagesViewController 库,我想在传入的 Buble/Message Outgoing Buble 工作的下方显示消息时间,在此处输入图像描述 这里是我的代码和那里将数据加载到 JSQMessages CollectionView 的方法请帮助我你知道提前谢谢

//MARK: JSQMessages 数据源函数

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
    let data = messages[indexPath.row]
    //set text color
    if data.senderId == FUser.currentId() {
        cell.textView?.textColor = .black
    } else {
        cell.textView?.textColor = .white
    }
    return cell
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
    return messages[indexPath.row]
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {

    let data = messages[indexPath.row]
    if data.senderId == FUser.currentId() {
        return outgoingBubble
    } else {
        return incomingBubble
    }
}



override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForCellTopLabelAt indexPath: IndexPath!) -> NSAttributedString! {
    if indexPath.item % 5 == 0 {
        let message = messages[indexPath.row]
        return JSQMessagesTimestampFormatter.shared()?.attributedTimestamp(for: message.date)
    }
    return nil
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForCellTopLabelAt indexPath: IndexPath!) -> CGFloat {

    if indexPath.item % 3 == 0 {
        return kJSQMessagesCollectionViewCellLabelHeightDefault
   }

    return 0.0
}



override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForCellBottomLabelAt indexPath: IndexPath!) -> NSAttributedString! {
    let message = objectMessages[indexPath.row]
     let messageDate = messages[indexPath.row]
    print("date is \( messageDate.date!)")
    let currentDateFormat = dateFormatter()
    dateFormatter().timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
    currentDateFormat.dateFormat = "hh:mm,a"
   let date = currentDateFormat.string(from:  messageDate.date!)
    var status: NSAttributedString!
    switch message[kSTATUS] as! String {
    case kDELIVERED:
        let attributedStringColors = [NSAttributedString.Key.foregroundColor : UIColor.darkGray]
       let statusText = "\(date)   ✔︎✔︎  "
        status = NSAttributedString(string: statusText, attributes: attributedStringColors)
    case kREAD:
         let color = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
        let attributedStringColors = [NSAttributedString.Key.foregroundColor :color]
        if message[kREADDATE]  != nil{

            let statusText =  "\(date)  ✔︎✔︎  "
            status = NSAttributedString(string: statusText, attributes: attributedStringColors)
        }
    default:
        status = NSAttributedString(string: "\(date)   ✔︎")

    }

    return status
}


func readTimeFrom(dateString: String) -> String {

    let date = dateFormatter().date(from: dateString)

    let currentDateFormat = dateFormatter()
    dateFormatter().timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
    currentDateFormat.dateFormat = "hh:mm,a"

    return currentDateFormat.string(from: date!)

}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForCellBottomLabelAt indexPath: IndexPath!) -> CGFloat {

    let data = messages[indexPath.row]

    if data.senderId == FUser.currentId() {
        return kJSQMessagesCollectionViewCellLabelHeightDefault
    } else {
        return 0.0
    }
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {

    let message = messages[indexPath.row]
    var avatar: JSQMessageAvatarImageDataSource
    print("avattar image ")
    if let testAvatar = jsqAvatarDictionary!.object(forKey: message.senderId) {
        avatar = testAvatar as! JSQMessageAvatarImageDataSource
    } else {
        avatar = JSQMessagesAvatarImageFactory.avatarImage(with: UIImage(named: "avatarPlaceholder"), diameter: 70)
    }

    return avatar
}

//for see users profile
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapAvatarImageView avatarImageView: UIImageView!, at indexPath: IndexPath!) {

    let senderId = messages[indexPath.row].senderId
    var selectedUser: FUser?

    if senderId == FUser.currentId() {
        selectedUser = FUser.currentUser()
    } else {
        for user in withUsers {
            if user.objectId == senderId {
                selectedUser = user
            }
        }
    }
    presentUserProfile(forUser: selectedUser!)
}


//for multimedia messages delete option
override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {

    super.collectionView(collectionView, shouldShowMenuForItemAt: indexPath)
    return true
}

override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {

    if messages[indexPath.row].isMediaMessage {
        if action.description == "delete:" {
            return true
        } else {
            return false
        }
    } else {
        if action.description == "delete:" || action.description == "copy:" {
            return true
        } else {
            return false
        }
    }
}


override func collectionView(_ collectionView: JSQMessagesCollectionView!, didDeleteMessageAt indexPath: IndexPath!) {

    let messageId = objectMessages[indexPath.row][kMESSAGEID] as! String

    objectMessages.remove(at: indexPath.row)
    messages.remove(at: indexPath.row)

    //delete message from firebase
    OutgoingMessage.deleteMessage(withId: messageId, chatRoomId: chatRoomId)
}*

标签: swiftjsqmessagesviewcontroller

解决方案


推荐阅读