【问题标题】:How to select and delete messages like whatsapp or iMessage on ios using swift如何使用swift在ios上选择和删除诸如whatsapp或iMessage之类的消息
【发布时间】:2017-01-28 17:53:44
【问题描述】:

您好,我正在尝试构建一个简单的聊天应用程序,用户可以在其中发送消息和照片。我很难找出在长按单条消息时选择和删除多条消息的最佳方法。

我使用集合视图来显示页面。现在我正在使用集合视图 didSelect 方法单击聊天气泡图像视图的一侧,并能够获取该特定单元格的选择按钮。但是,我无法为每条消息附加复选框按钮。我也不能长按聊天气泡图像视图。

我也尝试过在聊天气泡上点击 imageview,但是我需要重新加载集合视图。有没有实现删除多条消息的最佳方法?

感谢任何帮助

谢谢

下面是示例代码

用于更改特定单元格的复选框图像的代码。

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")
    }

点击聊天气泡的代码以将复选框按钮附加到所有单元格。

 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
    }}

当点击聊天气泡时,集合视图会重新加载以将复选框按钮附加到所有单元格

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

我最终要做的是选择和删除像 whatsapp 或 iMessage(上面的代码接近 iMessage 功能)这样的消息。所以我也完全愿意接受完整的代码更改。谢谢。

更新代码

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")
    }
}

长按复选框会出现在所有可见单元格上,但点击聊天气泡不起作用。

【问题讨论】:

  • 你可以使用这篇文章stackoverflow.com/questions/29241691/…的建议来完成长按功能
  • 谢谢@danielmhanover。我仍然无法在聊天气泡上进行选择(如果我触摸聊天气泡旁边,选择正在工作)。
  • 聊天气泡是uiimageview吗?请务必将 userInteractionEnabled 设置为 true
  • 什么是cell.checkbox?图像?按钮?
  • @MohsenHosseinpour 它的 uiimage

标签: ios swift3


【解决方案1】:

您应该将UILongPressGestureRecognizer 附加到collectionview 中的每个单元格,并将UICollectionviewcontroller 设置为每个识别器的目标。然后,当其中任何一个触发时,将 CollectionViewController 的自定义属性(可能将其命名为 editing 或其他名称)设置为 true。然后使用 UICollectionView 的 visibleCells 函数获取所有可见单元格。

在您的 UICollectionViewCell 子类中,您应该有一些自定义属性 getter/setter 方法(可能是 -editing-setEditing:(BOOL)),您现在可以在遍历 visibleCells 中的单元格时调用它们。在您的 -setEditing:(BOOL) 函数中,您可以随意添加和删除复选框 UIButton。您还需要将 UICollectionView 控制器设置为此 UIButton 的目标,并在 UICollectionViewController 中跟踪选择了哪些单元格,以便当用户点击“删除”按钮时,您知道要删除哪些消息。

我还建议您查看https://github.com/jessesquires/JSQMessagesViewController/,它会为您完成所有这些逻辑。

【讨论】:

  • 首先,感谢您花时间回答我的问题。我已启用聊天气泡 userInteraction 并添加了 UILongPressGestureRecognizer。我正在使用 UILongPressGesture 将复选框按钮添加到单元格和 collectionview didselect,diddeselect 以选择或取消选择复选按钮。但是在聊天气泡上选择仍然不起作用
  • longPress 工作正常,但点击选择不工作?
  • 也许使用 UITapGestureRecognizer?
  • 我也尝试过 UITapGestureRecognizer 但由于它已经有 UILongPressRecognizer 我猜它不起作用。我也尝试将图像视图更改为按钮,但它不起作用
  • 尝试在手势识别器上将 developer.apple.com/reference/uikit/uigesturerecognizer/… 设置为 NO
猜你喜欢
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 2013-04-03
相关资源
最近更新 更多