您好我正在尝试构建一个简单的聊天应用程序,用户可以在其中发送消息和照片。我很难确定在长按一条消息时选择和删除多条消息的最佳方式。
我使用了集合视图来显示页面。现在我正在使用集合视图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")
}
}
长按复选框会显示在所有可见单元格上,但点击聊天气泡不起作用。
答案 0 :(得分:1)
您应该为集合视图中的每个单元格附加UILongPressGestureRecognizer
,并将UICollectionviewcontroller设置为每个识别器的目标。然后,当其中任何一个触发时,将CollectionViewController的自定义属性(可能将其命名为editing
或其他)设置为true。然后使用UICollectionView的visibleCells
函数获取所有可见单元格。
在你的UICollectionViewCell子类中,你应该有一些自定义属性getter / setter方法(可能是-editing
和-setEditing:(BOOL)
),你可以在迭代visibleCells
中的单元格时调用它们。在-setEditing:(BOOL)
功能中,您可以根据需要添加和删除复选框UIButton。您还需要将UICollectionView控制器设置为此UIButton的目标,并在UICollectionViewController中跟踪选择的单元格,以便用户点击"删除"按钮,您知道要删除哪些消息。
我还建议您查看https://github.com/jessesquires/JSQMessagesViewController/,它会为您完成所有这些逻辑。