0

I have an tableview which list static array values. Multiple selection can be done in tableview. I am trying to show an UIView when selecting cell and hide none of the cell is selected, Also i want save selected cell values into an array, so that i can able to post data to api calls.

Here is my code:

 //UIView which i want to show (when cell is selected) and hide (when cell is not selected)
    @IBOutlet weak var rejAppView: UIView!

//tableView

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sampleData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "prCell", for: indexPath) as! PrListCellTableViewCell

    let data = sampleData[indexPath.row]
    print("req|:\(String(describing: data.req))")
    cell.reqLbl.text = data.req

    let ChecktapGesture = UITapGestureRecognizer(target: self, action: #selector(self.checktapBtnAction(_:)))
    cell.tickImageView.tag = indexPath.row
    cell.tickImageView.addGestureRecognizer(ChecktapGesture)
    cell.tickImageView.isUserInteractionEnabled = true

    let passReqtapGesture = UITapGestureRecognizer(target: self, action: #selector(self.passReqtapBtnAction(_:)))
    cell.passReqNo.tag = indexPath.row
    cell.passReqNo.addGestureRecognizer(passReqtapGesture)
    cell.passReqNo.isUserInteractionEnabled = true



    return cell
}

For multiple selection i have added tap gesture for imageView:

here is the code mutiple selection and UIVIiew show/hide:

    @objc func checktapBtnAction(_ sender: UITapGestureRecognizer) {

    print("\(String(describing: sender.view?.tag)) Tapped")


    guard let rowIndexPath = sender.view?.tag else {
        return
    }



    let indexPath = IndexPath(row: rowIndexPath, section: 0)

    let cell = tableView.cellForRow(at: indexPath) as! PrListCellTableViewCell

 //        if cell.checked == true {
 //
 //            self.rejAppView.isHidden = true
 // . 
 //
//        } else if cell.checked == false {

//            self.rejAppView.isHidden = false
//        }



    cell.checked = !cell.checked
    print("cell type\(String(describing: cell.checked))")

   }

Here is my tableView cell code:

       @IBOutlet weak var tickImageView: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    checked = false

}

var checked: Bool! {
    didSet {
        if (self.checked == true) {
            self.tickImageView.image = UIImage(named: "tick")
        }else{
            self.tickImageView.image = UIImage(named: "untick")
        }
    }
}

My issue i can able to show rejAppView (UIView) when selecting single/multiple cell but when unselecting all the cell rejAppView (UIView) is not hided also i could not able store selected value into an array. Any help much appreciated pls...

1 Answer 1

1

Define array of indexPath or of integer in which you add indexPath/ indexPath.row if cell selected. and remove that object on unselecting cell.

based on that array you can hide view

var array = [IndexPath]()


@objc func checktapBtnAction(_ sender: UITapGestureRecognizer) {
print("\(String(describing: sender.view?.tag)) Tapped")


guard let rowIndexPath = sender.view?.tag else {
    return
}
let indexPath = IndexPath(row: rowIndexPath, section: 0)


cell.checked = !cell.checked
print("cell type\(String(describing: cell.checked))")

if cell.checked {
    array.append(indexPath)
} else {
    let index = array.index(of: indexPath)
    array.remove(at: index)
}

if array.count == 0 {
    self.rejAppView.isHidden = true
} else {
    self.rejAppView.isHidden = false
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.