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...