如何旋转图像选定行和取消旋转图像未选定行

Posted

技术标签:

【中文标题】如何旋转图像选定行和取消旋转图像未选定行【英文标题】:How to rotate image selected row and unrotate image unselected row 【发布时间】:2017-11-14 03:48:19 【问题描述】:

我有这样的问题:

我有一些行,当我单击第 0 行时,它会展开并且下拉图像会旋转。当我单击第 1 行时,它会展开第 1 行并取消展开第 0 行,第 0 行中的下拉图像恢复正常(向下箭头),第 1 行中的下拉图像旋转(向上箭头)。

到目前为止,这是我的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    let cell = self.tableView.cellForRow(at: indexPath) as! PaymentMethodCell
    let clickedRow = indexPath.row
    if indexPath.row == selectedRow
        print("yang muncul selectedRow = -1")
        if cell.isSelected 
            selectedRow = -1
            print("This cell is deselected :\(selectedRow)")
            cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) * 180.0)
        else
            selectedRow = indexPath.row
            cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) / 180.0)
        
    else
        if cell.isSelected 
            selectedRow = indexPath.row
            print("This cell is selected :\(selectedRow)")
            cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) / 180.0)
        else
            selectedRow = -1
            cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) * 180.0)
        
        cell.lblclickedsubcell.text="00"
        print("cell.lblclickedsubcell.text:\(String(describing: cell.lblclickedsubcell.text!))")
    
    tableView.reloadData()

但结果是在我展开第 0 行然后单击第 1 行、第 0 行未展开和第 1 行展开后。但是第 0 行的下拉图像仍然没有恢复正常(向下箭头)。它仍然是向上箭头。

这里是 cellForRowAt :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    var cell = tableView.dequeueReusableCell(withIdentifier: "PaymentMethodCell", for: indexPath) as! PaymentMethodCell
    cell.ivPaymentMeyhod.contentMode = UIViewContentMode.scaleAspectFit
    cell.ivPaymentMeyhod.contentMode = UIViewContentMode.left
    cell.ivPaymentMeyhod.clipsToBounds=false
    //cell.ivPaymentMeyhod.kf.setImage(with: url!)
    cell.ivPaymentMeyhod.image = UIImage(named: "\(PaymentImageArray[indexPath.item])")
    let clickedRow = indexPath.row
    cell.lblclickedRow.text = "\(indexPath.row)"
    let ContractNumber = "\(contractnumber!)"
    if(CustomUserDefaults.shared.isGuest() || ContractNumber=="")
        cell.lblContractNumber.text = ""
        cell.lblContractAmount.text = ""
    else
        cell.lblContractNumber.text = "\(contractnumber!)"
        cell.lblContractAmount.text = "\(contractamount!)"
    
    if(clickedRow==0)
        cell.tableView.isHidden=true
        cell.XibView.isHidden=false
        let AlfaIndoView = HowToPayView.AlfaIndoNib()
        let cellwidth = cell.XibView.bounds.width
        print("cellwidth:\(cellwidth)")
        cell.XibView.addSubview(AlfaIndoView)
        AlfaIndoView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        AlfaIndoView.frame.size.width = cellwidth
    else if(clickedRow==1)
        cell.tableView.isHidden=true
        cell.XibView.isHidden=false
        let POSindonesiaView = HowToPayView.POSindonesiaNib()
        let cellwidth = cell.XibView.bounds.width
        print("cellwidth:\(cellwidth)")
        cell.XibView.addSubview(POSindonesiaView)
        POSindonesiaView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        POSindonesiaView.frame.size.width = cellwidth
    else if(clickedRow==2)
        cell.tableView.isHidden=false
        cell.XibView.isHidden=true
        cell.setUpTable()
    else if(clickedRow==3)
        cell.tableView.isHidden=false
        cell.XibView.isHidden=true
        cell.setUpTable()
    else if(clickedRow==4)
        cell.tableView.isHidden=false
        cell.XibView.isHidden=true
        cell.setUpTable()
    else if(clickedRow==5)
        cell.tableView.isHidden=false
        cell.XibView.isHidden=true
        cell.setUpTable()
    else if(clickedRow==6)
        cell.tableView.isHidden=true
        cell.XibView.isHidden=false
        let ATMbersamaView = HowToPayView.ATMbersamaNib()
        let cellwidth = cell.XibView.bounds.width
        print("cellwidth:\(cellwidth)")
        cell.XibView.addSubview(ATMbersamaView)
        ATMbersamaView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        ATMbersamaView.frame.size.width = cellwidth
    
    return cell

帮帮我,请修复此代码

【问题讨论】:

【参考方案1】:

如果我们能看到您的cellForRow 代码会更清楚。据我所知,您最后调用了reloadData(),因此您的单元格正在重新渲染,因此您必须在cellForRow 代码中处理箭头的方向。所以在伪代码形式下,它会有点像以下:

didSelect函数中:

selectedIndexPath = indexPath
//...other related statements

cellForRow函数中:

if indexPath == selectedIndexPath 
    //rotate arrow down
    //other relevant code

else 
    //rotate arrow up
    //other relevant code

【讨论】:

以上是关于如何旋转图像选定行和取消旋转图像未选定行的主要内容,如果未能解决你的问题,请参考以下文章

如何在swift 3的表格视图中仅更改选定行和未选定行的字体颜色和大小?

如何取消选择 DataGridView 控件中的所有选定行?

如何以编程方式取消选择 Datatable 中的选定行

在表格视图中选择新行时如何取消选择选定的行?

Javascript jQuery插件DataTables取消选择元素

如何在确认框的取消事件后强制 asp.net 列表框保留选定的值?