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

时间:2017-11-14 03:48:19

标签: uitableview swift3 uiimage tableview

我有这样的问题:enter image description here

我有一些行,当我点击第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 个答案:

答案 0 :(得分:0)

如果我们能够看到您的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
}