我在点击按钮时使用UITableView我将tableview放入编辑模式我添加了
tableView.allowMultiselectionduringedit = true
在这里,我想要选择表格,但点击时不会添加复选标记。但是didselectrow中的代码正在执行
我应该更改什么才能选择行
didselect的代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Did select Run")
if(isEditing){
let cell = chatTableView.cellForRow(at: indexPath)
cell?.setSelected(true, animated: true)
}else{
let groupMsg = groupMsgList[indexPath.row]
if(groupMsg.messageType == "file")
{
let fileNameString = groupMsg.fileOriginalName
if(fileNameString != nil){
let fileName: NSString = fileNameString! as NSString
let extention = fileName.pathExtension.lowercased()
switch extention {
case "jpg","png","jpeg":
if (groupMsg.senderUserId != LoginStatusInfo.userId){
let row = chatTableView.cellForRow(at: indexPath) as! PhotoReciveCell
chatTableView.beginUpdates()
row.downloadLabel.text = " Downloading... "
row.downloadLabel.sizeToFit()
row.downloadLabelConstraint.constant = 30.0
row.downloadLabel.layoutMargins = UIEdgeInsets(top: 2.0, left: 2.0, bottom: 2.0, right: 2.0)
downloadFileBackground(id: groupMsg.messageId!, fileName: groupMsg.message!, cellIndexPath: indexPath )
chatTableView.endUpdates()
}else{
let row = chatTableView.cellForRow(at: indexPath) as! PhotoSentCell
chatTableView.beginUpdates()
row.downloadLabel.text = " Downloading... "
row.downloadLabel.sizeToFit()
row.downloadLabelConstraint.constant = 30.0
row.downloadLabel.layoutMargins = UIEdgeInsets(top: 2.0, left: 2.0, bottom: 2.0, right: 2.0)
downloadFileBackground(id: groupMsg.messageId!, fileName: groupMsg.message!, cellIndexPath: indexPath )
chatTableView.endUpdates()
}
break
default :
if (groupMsg.senderUserId != LoginStatusInfo.userId){
let row = chatTableView.cellForRow(at: indexPath) as! FileRecieveCell
chatTableView.beginUpdates()
row.downloadStatusIndicator.isHidden = true
row.downloadingIndicator.alpha = 1.0
chatTableView.endUpdates()
}else{
let row = chatTableView.cellForRow(at: indexPath) as! FileSentCell
chatTableView.beginUpdates()
row.downloadStatusIndicator.isHidden = true
row.downloadingIndicator.alpha = 1.0
chatTableView.endUpdates()
}
downloadFileBackground(id: groupMsg.messageId!, fileName: groupMsg.fileOriginalName!, cellIndexPath: indexPath )
break
}
}
}
print(indexPath)
}
}
对于CellatRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let groupMsg = groupMsgList[indexPath.row]
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(_:)))
groupId = groupMsg.groupId
//MARK: MessageType is textmessage
if (groupMsg.messageType == "message")
{
if (groupMsg.senderUserId != LoginStatusInfo.userId)
{
let cell : RecievedMessageCell = chatTableView.dequeueReusableCell(withIdentifier: "recieve") as! RecievedMessageCell
cell.name.text = groupMsg.senderDisplayName
cell.message.text = groupMsg.message
cell.time.text = groupMsg.dateSent!
// cell.message.sizeToFit()
cell.receiveMessageView.tag = indexPath.row
cell.receiveMessageView.addGestureRecognizer(longPressGesture)
return cell
}
else
{
let cell : SentMessageCell = chatTableView.dequeueReusableCell(withIdentifier: "send") as! SentMessageCell
// cell.sentMessageView.roundCorners(corners: [.topLeft,.topRight,.bottomLeft], radius: 20)
// cell.sentMessageView.layer.masksToBounds = false
cell.sentMessageView.layer.cornerRadius = 10
cell.message.text = groupMsg.message
cell.time.text = groupMsg.dateSent!
cell.messageStatus.isHidden = false
cell.messageStatus.text = groupMsg.status
// cell.message.sizeToFit()
cell.sentMessageView.tag = indexPath.row
cell.sentMessageView.addGestureRecognizer(longPressGesture)
return cell
}
}
//MARK: Message type is Banner
else if (groupMsg.messageType == "banner") {
let cell : BotBanner = chatTableView.dequeueReusableCell(withIdentifier: "botBanner") as! BotBanner
hasBanner = true
scrollToIndex = indexPath.row
return cell
}
//MARK: Message type is File
else{
if (groupMsg.senderUserId != LoginStatusInfo.userId)
{
//MARK: File Recived
let fileNameString = groupMsg.fileOriginalName
if(fileNameString != nil){
let fileName: NSString = fileNameString! as NSString
let extention = fileName.pathExtension.lowercased()
let cell : FileRecieveCell = chatTableView.dequeueReusableCell(withIdentifier: "fileRecieve") as! FileRecieveCell
let fileUrl = self.getImageURL(forFileName: fileNameString!)
if(fileUrl == nil){
cell.downloadStatusIndicator.image = #imageLiteral(resourceName: "fileDonwloadBlack")
}else{
cell.downloadStatusIndicator.image = #imageLiteral(resourceName: "downloadedBlack")
}
cell.downloadStatusIndicator.isHidden = false
cell.downloadingIndicator.alpha = 0.0
cell.receiveFileView.layer.cornerRadius = 10
cell.name.text = groupMsg.senderDisplayName
cell.message.text = groupMsg.message
cell.time.text = groupMsg.dateSent!
cell.fileIcon.image = returnImage(fileName:groupMsg.message!)
cell.receiveFileView.isUserInteractionEnabled = true
cell.receiveFileView.addGestureRecognizer(longPressGesture)
cell.receiveFileView.tag = indexPath.row
return cellname.text = groupMsg.senderDisplayName
cell.message.text = groupMsg.message
cell.time.text = groupMsg.dateSent!
cell.fileIcon.image = returnImage(fileName:groupMsg.message!)
cell.receiveFileView.isUserInteractionEnabled = true
cell.receiveFileView.addGestureRecognizer(longPressGesture)
cell.receiveFileView.tag = indexPath.row
return cell
}
}
else
{
let cell : FileSentCell = chatTableView.dequeueReusableCell(withIdentifier: "fileSend") as! FileSentCell
let fileUrl = self.getImageURL(forFileName: fileNameString!)
if(fileUrl == nil){
cell.downloadStatusIndicator.image = #imageLiteral(resourceName: "fileDownloadWhite")
}else{
cell.downloadStatusIndicator.image = #imageLiteral(resourceName: "downloadedWhite")
}
cell.downloadStatusIndicator.isHidden = false
cell.downloadingIndicator.alpha = 0.0
cell.sendFileView.layer.cornerRadius = 10
cell.name.text = groupMsg.senderDisplayName
cell.message.text = groupMsg.message
cell.time.text = groupMsg.dateSent!
cell.fileStatus.text = groupMsg.status
cell.fileIcon.image = returnImage(fileName:groupMsg.message!)
cell.sendFileView.tag = indexPath.row
cell.sendFileView.addGestureRecognizer(longPressGesture)
cell.sendFileView.isUserInteractionEnabled = true
return cell
}
}
}
}
答案 0 :(得分:0)
Tableview使用dequeueReusableCell,所以你可以
cell?.setSelected(true, animated: true)
滚动tableview时,将选择从单元格中重复使用的相同单元格 - >错了。
在GroupMessage
模型中添加属性:isSelected
。并设置
isSelected = true
self.tableview.reloadrowatindexpath ....
何时被选中
在cellForRowAt indexPath
If groupMsg.isSelected{
cell?.setSelected(true, animated: true)
}else{
cell?.setSelected(false, animated: true)
}
或者创建一个NSMutableArray - selectedMessage
来保留选中的消息didSelected在selectedMessage
中添加/删除groupmessage
在cellForRowAt indexPath
检查selectedMessage
包含groupmessage - setSelected = true或no
答案 1 :(得分:0)
解决!
我的错误是我需要在cellForAtRow中添加cell.setSelectionStype = .default