1

Hello I am trying build a simple chat application where users can send messages and photos. I am having a hard time in figuring out the best way to select and delete multiple messages on long press on a single message.

I have used collection view to display the page. Right now I am using collection view didSelect method to click on the side of chat bubble image view and able to get select button for that particular cell. But, I am not able append checkbox button for every message. I also cannot long press on the chat bubble image view.

I also tried imageview tap on chat bubble but with this I need to reload the collection view. Is there a best way of implementing delete multiple messages?

Any help is appreciated

Thanks

Below is the sample code

code for changing the checkbox image of particular cell.

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    inputTextField.endEditing(true)
    let cell: ChatLogMessageCell? = collectionView.cellForItem(at: indexPath) as! ChatLogMessageCell?
    cell?.checkbox.isHidden = false
    selectAll = true
   if cell?.isSelected == true{

        cell?.checkbox.image = UIImage(named: "checkedimage")
    }else{

        cell?.checkbox.image =  UIImage(named: "uncheckedimage")
    }

code to tap on chat bubble to append checkbox button to all cells.

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: chatcellId, for: indexPath) as! ChatLogMessageCell
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped))
    cell.bubbleImageView.addGestureRecognizer(tapGesture)
    cell.bubbleImageView.isUserInteractionEnabled = true

    if selectAll == true{
        cell.checkbox.isHidden = false
    }else{
        cell.checkbox.isHidden = true
    }}

When chat bubble is tapped collection view is reloaded to append the checkbox button to all cells

func imageTapped(){
    selectAll = true
    self.collectionView?.reloadData()
}

What I am finally trying to do is select and delete messages like whatsapp or iMessage (Above code is close to iMessage functionality) does. So I am completely open for complete code changes too. Thanks.

updated Code

override func viewDidLoad() 
{
    super.viewDidLoad()
    let lpgr = UILongPressGestureRecognizer(target: self, 
                   action: #selector(handleLongPress))
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    lpgr.delegate = self
    self.collectionView?.addGestureRecognizer(lpgr)

}

func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
    let p = gestureReconizer.location(in: self.collectionView)
    let indexPath = self.collectionView?.indexPathForItem(at: p)

    if let index = indexPath {
        let cell: ChatLogMessageCell? = collectionView?.cellForItem(at: index) as! ChatLogMessageCell?
        self.collectionView?.allowsMultipleSelection = true

        for cell in collectionView!.visibleCells as! [ChatLogMessageCell] {
             let indexPath = collectionView?.indexPath(for: cell as ChatLogMessageCell)

                cell.checkbutton.isHidden = false

            if selectedMsgs.contains((messages?[((indexPath)?.item)!])!) {
                cell?.checkbox.image = UIImage(named: "checkedimage")
            }
            else {
                cell?.checkbox.image =  UIImage(named: "uncheckedimage")
            }
        }


    } else {
        print("Could not find index path")
    }
}

On long press check boxes appear on all visible cells, but tap on chat bubble is not working.

Shravya
  • 208
  • 3
  • 11

1 Answers1

1

You should attach a UILongPressGestureRecognizer to each cell in the collectionview, and set the UICollectionviewcontroller as the target for each of these recognizers. Then, when any one of them fires, set a custom property of your CollectionViewController (maybe name it editing or something) to true. Then fetch all the visible cells with the UICollectionView's visibleCells function.

In your UICollectionViewCell subclass, you should have some custom property getter/setter methods (maybe -editing and -setEditing:(BOOL)) which you can call now as you iterate through the cells in visibleCells. Within your -setEditing:(BOOL) function, you can add and remove the checkbox UIButton as you please. You'll also want to set the UICollectionView controller as the target of this UIButton, and within the UICollectionViewController, keep track of which cells are selected so when the user hits the "Delete" button, you know which messages to delete.

I would also recommend checking out https://github.com/jessesquires/JSQMessagesViewController/, which does all this logic for you.

danielmhanover
  • 3,094
  • 4
  • 35
  • 51
  • Firstly, Thank you for taking time to answer my question.I have made the chat bubble userInteraction enabled and added UILongPressGestureRecognizer. I am using UILongPressGesture to add checkbox buttons to cells and collectionview didselect, diddeselect to select or unselect the checkbuttons. But select on chat bubble is still not working – Shravya Jan 28 '17 at 20:04
  • the longPress is working but the tap to select is not? – danielmhanover Jan 29 '17 at 17:14
  • Perhaps use a UITapGestureRecognizer? – danielmhanover Jan 29 '17 at 19:11
  • I tried UITapGestureRecognizer too but as it already has UILongPressRecognizer I guess it is not working. I also tried changing image view to button and it didn't work – Shravya Jan 29 '17 at 20:09
  • Try setting https://developer.apple.com/reference/uikit/uigesturerecognizer/1624218-cancelstouchesinview to NO on the gesture recognizers – danielmhanover Jan 29 '17 at 20:10
  • I couldn't really understand the above comment on how to implement it. So, I updated my question with the latest code . Please do look at it and if you find anything wrong in the code, please let me know. I would really appreciate if you could explain more about above comment on how to use it in my case if that's the only way to solve this issue. Thanks. – Shravya Jan 30 '17 at 17:28