通过设置某种标志来隐藏imageView和Label

时间:2018-07-10 15:11:57

标签: ios swift uitableview swift3

如果来自同一用户的消息,你们能给我一些帮助以隐藏图像和名称吗?我只想在第一条消息中显示它,并且如果该用户发送了更多信息而不再显示直到出现另一个ID ...就像whatsapp一样。

目前,我正在向您展示一个示例 [![在此处输入图片描述] [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

1 个答案:

答案 0 :(得分:1)

您需要像检查每条消息一样检查以前的聊天消息ID

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
}