2017-06-29 72 views
0

我正在嘗試製作一個類似於「消息應用程序」的應用程序。您可以發送和接收消息(有短信和圖像消息)。問題是,當我發送圖像時沒問題,但是當我發送另一張圖像時,兩張圖像都變成等於我發送的最後一張圖像。我的「消息應用程序」中的圖像消息的問題

主控制器(消息所在的地方)是一個CollectionViewController,它有一個「ChatMessageCell」類。

我把代碼放在下面。我希望你能幫助我......謝謝你!

var messages = [Message]() 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ChatMessageCell 

    let message = messages[indexPath.item] 
    cell.textView.text = message.text 


    setupCell(cell, message: message) 

    if let text = message.text { 
     //a text message 
     cell.bubbleWidthAnchor?.constant = estimateFrameForText(text).width + 32 
     cell.textView.isHidden = false 

    } else if message.text == nil { 
     //fall in here if its an image message 
     cell.bubbleWidthAnchor?.constant = 200 
     cell.textView.isHidden = true 
    } 

    return cell 
} 


fileprivate func setupCell(_ cell: ChatMessageCell, message: Message) { 

    if message.id == 1 { 
     //outgoing blue 

     cell.bubbleViewRightAnchor?.isActive = true 
     cell.bubbleViewLeftAnchor?.isActive = false 

     if message.text != nil { 
      //text message 
      cell.bubbleView.backgroundColor = ChatMessageCell.blueColor 
      cell.textView.textColor = UIColor.white 
      cell.messageImageView.isHidden = true 

     } else { 
      //image message 
      cell.messageImageView.image = imagenSeleccionada 
      cell.messageImageView.isHidden = false 
      cell.bubbleView.backgroundColor = UIColor.clear 
     } 


    } else if message.id == 2 { 
     //incoming gray 

     cell.bubbleViewRightAnchor?.isActive = false 
     cell.bubbleViewLeftAnchor?.isActive = true 

     if message.text != nil { 
      //mensaje de texto 
      cell.bubbleView.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0) 
      cell.textView.textColor = UIColor.black 
      cell.messageImageView.isHidden = true 

     } else { 
      //mensaje de imagen 
      cell.messageImageView.image = imagenSeleccionada 
      cell.messageImageView.isHidden = false 
      cell.bubbleView.backgroundColor = UIColor.clear 
     } 

    } 

} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 

    var message: Message? 

    if swiche.isOn == true { 

     message = Message(text: self.inputTextField.text, id: 1, imagen: nil, imageHeight: nil, imageWidth: nil) 

    } else { 

     message = Message(text: self.inputTextField.text, id: 2, imagen: nil, imageHeight: nil, imageWidth: nil) 
    } 

    messages.append(message!) 
    self.inputTextField.text = nil 
    collectionView?.reloadData() 
    return true 
} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    //we selected an image 
    handleImageSelectedForInfo(info as [String : AnyObject]) 


    dismiss(animated: true, completion: nil) 
} 


private func handleImageSelectedForInfo(_ info: [String: AnyObject]) { 

    var selectedImageFromPicker: UIImage? 

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage { 
     selectedImageFromPicker = editedImage 

    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage { 

     selectedImageFromPicker = originalImage 
    } 

    if let selectedImage = selectedImageFromPicker { 

     imagenSeleccionada = selectedImage 
    } 


    var message: Message? 

    if swiche.isOn == true { 
     //azul 
     message = Message(text: nil, id: 1, imagen: imagenSeleccionada, imageHeight: imagenSeleccionada?.size.height as NSNumber?, imageWidth: imagenSeleccionada?.size.width as NSNumber?) 

    } else { 

     message = Message(text: nil, id: 2, imagen: imagenSeleccionada, imageHeight: imagenSeleccionada?.size.height as NSNumber?, imageWidth: imagenSeleccionada?.size.width as NSNumber?) 
    } 

    messages.append(message!) 
    collectionView?.reloadData() 

} 

「消息」 類

struct Message { 

var text: String? 

//1 = blue ; 2 = gris 
var id: NSNumber? 
var imagen: UIImage? 

var imageHeight: NSNumber? 
var imageWidth: NSNumber? 

}

回答

1

在你setupCell方法(類型NSObject的),要設置的ImageView的具有存儲在變量imagenSeleccionada圖像。所以要解決你的問題,用這個來替換你的setupCell方法

func setupCell(_ cell: ChatMessageCell, message: Message) { 

    if message.id == 1 { 
     //outgoing blue 

     cell.bubbleViewRightAnchor?.isActive = true 
     cell.bubbleViewLeftAnchor?.isActive = false 

     if message.text != nil { 
      //text message 
      cell.bubbleView.backgroundColor = ChatMessageCell.blueColor 
      cell.textView.textColor = UIColor.white 
      cell.messageImageView.isHidden = true 

     } else { 
      //image message 
      cell.messageImageView.image = Message.imagen 
      cell.messageImageView.isHidden = false 
      cell.bubbleView.backgroundColor = UIColor.clear 
     } 


    } else if message.id == 2 { 
     //incoming gray 

     cell.bubbleViewRightAnchor?.isActive = false 
     cell.bubbleViewLeftAnchor?.isActive = true 

     if message.text != nil { 
      //mensaje de texto 
      cell.bubbleView.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0) 
      cell.textView.textColor = UIColor.black 
      cell.messageImageView.isHidden = true 

     } else { 
      //mensaje de imagen 
      cell.messageImageView.image = Message.imagen 
      cell.messageImageView.isHidden = false 
      cell.bubbleView.backgroundColor = UIColor.clear 
     } 

    } 

} 
+0

是的,那很好!謝謝!!! – Edwjonn

相關問題