Mohamed Lee
Mohamed Lee

Reputation: 267

Hide imageView and Label by setting some kind of flag

Can you guys give me some help to hide the image and name if the message it's from the same user... I want only to show it for the first message...and if that user send more not to show anymore until another Id appear... like whatsapp does it..

currently I m having like this to show u an example [![enter image description here][1]][1]

var isTheSameUser = false

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let selectedChat = chat[indexPath.row]
    let myId = String(describing:UserDefaults.standard.value(forKey: "user_id")!)
    let messageUserId = String(describing:selectedChat.owner_id)

    if messageUserId == myId {
        return myMessageCell(indexPath: indexPath)
    }else {
        return userMessageCell(indexPath: indexPath)
    }
}




func myMessageCell(indexPath :IndexPath) -> UITableViewCell {
    let cell = self.mainTableView.dequeueReusableCell(withIdentifier: "MyMessageTableViewCell", for: indexPath) as! MyMessageTableViewCell
    let selectedChat = self.chat[indexPath.row]
    let myId = String(describing:UserDefaults.standard.value(forKey: "user_id")!)

    // Show only for the first message
    // photo image
    if !isTheSameUser {
        cell.profileImageView.isHidden = false
        cell.profileNameLabel.isHidden = false

    } else {
        cell.profileImageView.isHidden = true
        cell.profileNameLabel.isHidden = true
    }

    if let userInfo = getUserMemberOf(userId: messageUserId) {

        cell.profileImageView.imageFromURL(urlString: userInfo["photo"] as! String)
    } else {
        cell.profileImageView.image = #imageLiteral(resourceName: "accountIcon")
    }
    cell.profileNameLabel.text = "\(String(describing: cell.userProfileInfo!["name"]!))"

    return cell

Upvotes: 0

Views: 142

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need check the previous chat messsage id like for every message

if indexPath.row != 0 {

   let prevItem = chat[indexPath.row - 1]
   let currentItem = chat[indexPath.row] 
    if prevItem.owner_id! == currentItem.owner_id! {
            // hide label and image
    }else {
        // show them

    }

}
else {
       // show them
}

Upvotes: 1

Related Questions