Reload tableview after clicking the button present inside one of the tableview cell in swift

Viewed 802

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.

1 Answers

Very simple to solve it,

every time you click a button, you record the index in the controller, then reload the tableView.

in the cell configuration, if the cell's indexPath matches the record index, its sub table show ,the rest's sub table hide.

If you hit the button two times, the record index in the controller is nil, then reload tableView, and all cell's sub table hides.


I name the pattern, mark & config


Your code is of chaos

from your code , I think LcodeTableViewCell is an object , not a class.

@IBAction func dropDown2ButtonAction(_ sender: UIButton){
    
    if buttonTag == getIndexPath()!.row{

buttonTag == getIndexPath()!.row, will be always true.

// BAD CODE ...

Related