【发布时间】: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