I have a tableview with numbers of cell. Each cell has a button. By clicking the button a dropdown box will open. I want when I will click on the button of a particular cell the dropdown box of the particular cell will show and all other dropdown of different cells will be hidden.
My code is
@IBAction func dropDown2ButtonAction(_ sender: UIButton){
if buttonTag == getIndexPath()!.row{
if isdropDownSelected2{
isdropDownSelected2 = false
}
else{
isdropDownSelected2 = true
}
}
else{
if let index = getIndexPath(){
buttonTag = index.row
}
isdropDownSelected2 = false
delegate?.updateTableView(completion: { [self] (updated) in
if updated{
isdropDownSelected2 = true
}
})
}
}
To get indexpath
func getIndexPath() -> IndexPath? {
guard let superView = self.superview as? UITableView else {
print("superview is not a UITableView - getIndexPath")
return nil
}
let indexPath = superView.indexPath(for: self)
return indexPath
}
code to show or hide my tableView
var isdropDownSelected2 = false{
didSet{
if isdropDownSelected2 == true{
showTable2()
}
else{
hideTable2()
}
}
}
private func showTable2(){
tableView2.isHidden = false
}
private func hideTable2(){
tableView2.isHidden = true
}
Code to update my tableview
func updateTableView(completion: @escaping (Bool) -> Void) {
tableView.reloadData {
completion(true)
}
}
extension UITableView {
func reloadData(completion:@escaping ()->()) {
UIView.animate(withDuration: 0, animations: { self.reloadData() })
{ _ in completion() }
}
}
In my table view cellFor row at indexpath function
LcodeTableViewCell?.dropDownBox2.tag = indexPath.row
LcodeTableViewCell?.delegate = self
if LcodeTableViewCell?.tableView1.isHidden == false{
LcodeTableViewCell?.tableView1.isHidden = true
}
if LcodeTableViewCell?.tableView2.isHidden == false{
LcodeTableViewCell?.tableView2.isHidden = true
}
Here buttonTag is a variable
var buttonTag = 0
I am doing Anything wrong here? I thing the tableview reloading problem is here.