shahtaj khalid shahtaj khalid - 1 year ago 45
iOS Question

Interactive Table View Header with check boxes

I designed the header of the tableview in such a way that it is being interactive through check boxes made from button by changing images.
What is happening: When i clicked a check box it is checked and unchecked correctly.
what i what: is when i click the check box, if it gets checked the the rest of the two checkboxes should get unchecked automatically. below is the code.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let identifierHeader:String = "SavedCallHeader"

let headerCell = tableView.dequeueReusableCell(withIdentifier: identifierHeader) as! SavedCallHeader




headerCell.checkBox_PlannedisOn = checkBox_PlannedisOn
headerCell.checkBox_unPlannedisOn = checkBox_unPlannedisOn
headerCell.checkBox_BothisOn = checkBox_BothisOn

headerCell.callBackForReload = { [weak self] (type,isCalled) -> Void in
print("is called Planned = \(type)")
print("is called Planned = \(isCalled)")

if type == "Planned"{
//Planned CallBack
if isCalled == true {
print("Planned selected")
self?.fetchPlannedData()
self?.tableView.reloadData()


} else {

self?.DeSelect()
self?.tableView.reloadData()
print("Planned Not selected")
}
self?.checkBox_PlannedisOn = isCalled
}else if type == "UnPlanned"{
//UnPlanned CallBack
if isCalled == true {
print("UnPlanned Selected")
self?.fetchUnPlannedData()
self?.tableView.reloadData()

} else {
self?.DeSelect()
self?.tableView.reloadData()
print("UnPlanned Not selected")
}
self?.checkBox_unPlannedisOn = isCalled
}else{
//Both CallBack
if isCalled == true {
print("Both Selected")
self?.fetchDate()
self?.tableView.reloadData()

} else {
self?.DeSelect()
self?.tableView.reloadData()
print("both Not selected")
}
self?.checkBox_BothisOn = isCalled
}

}

return headerCell

}


and this is the custom header class

class SavedCallHeader : UITableViewCell{

var callBackForReloadPlanned : ((Bool) -> ())?
var callBackForReloadBoth : ((Bool) -> ())?
var callBackForReloadUnPlanned : ((Bool) -> ())?

var NameOfOnStatus : ((String) -> ())?

var AnyOneIsOn:Bool = true

var callBackForReload : ((String,Bool) -> ())?

// var checkBox_PlannedisOn:Bool = false
// var checkBox_BothisOn:Bool = true
// var checkBox_unPlannedisOn:Bool = false

let defaults = UserDefaults.standard

@IBOutlet weak var PlannedBoxBtn: UIButton!
@IBOutlet weak var BothBoxBtn: UIButton!
@IBOutlet weak var unPlannedBoxBtn: UIButton!

override func awakeFromNib() {
super.awakeFromNib()
}

var checkBox_PlannedisOn:Bool = false {
didSet{
self.PlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
if checkBox_PlannedisOn{
self.PlannedBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal)
// self.BothBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
// self.unPlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
}
}
}

var checkBox_BothisOn:Bool = false {
didSet{
self.BothBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
if checkBox_BothisOn{
self.BothBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal)
// self.unPlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
// self.PlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
}
}
}

var checkBox_unPlannedisOn:Bool = false {
didSet{
self.unPlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
if checkBox_unPlannedisOn{
self.unPlannedBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal)
// self.PlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
// self.BothBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
}
}
}

@IBAction func PlannedCheckBox(_ sender: UIButton) {

if checkBox_PlannedisOn == false {
// self.PlannedBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal)
checkBox_PlannedisOn = true
print("i'm finally here proper click!",checkBox_PlannedisOn)
self.callBackForReload!("Planned",true)


}else {
// self.PlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
checkBox_PlannedisOn = false
print("i'm finally heress proper click!",checkBox_PlannedisOn)
self.callBackForReload!("Planned",false)
}

}

@IBAction func BothCheckBox(_ sender: UIButton) {


if checkBox_BothisOn == false {
// self.BothBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal)
checkBox_BothisOn = true
self.callBackForReload!("Both",true)


}else {
// self.BothBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
// self.BothBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
checkBox_BothisOn = false
self.callBackForReload!("Both",false)
}

}

@IBAction func UnPlannedCheckBox(_ sender: Any) {


if checkBox_unPlannedisOn == false {
// self.unPlannedBoxBtn.setImage(UIImage(named: "Checked Checkbox-26.png"), for: UIControlState.normal)
checkBox_unPlannedisOn = true
self.callBackForReload!("UnPlanned",true)


}else {
// self.unPlannedBoxBtn.setImage(UIImage(named: "Unchecked Checkbox-26.png"), for: UIControlState.normal)
checkBox_unPlannedisOn = false
self.callBackForReload!("UnPlanned",false)

}
}
}


This needs to be sorted very urgent and Fast help will be aprrecicated Thanks!!

Answer Source

Declare a variable to keep track of the selected checkbox and update its value depending on that variable's status.

Inside func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? modify the code like

var selectedCheckBoxIndex:Int?



headerCell.checkBox_PlannedisOn = selectedCheckBoxIndex == 0 ? true : false
    headerCell.checkBox_unPlannedisOn = selectedCheckBoxIndex == 1 ? true : false
    headerCell.checkBox_BothisOn = selectedCheckBoxIndex == 2 ? true : false


if type == "Planned"{
            //Planned CallBack
            if isCalled == true {
                 print("Planned selected")
                self?.fetchPlannedData()
                self?.tableView.reloadData()
               selectedCheckBoxIndex = 0

            } else {

                self?.DeSelect()
                self?.tableView.reloadData()
                print("Planned Not selected")
            }
            self?.checkBox_PlannedisOn = isCalled
        }else if type == "UnPlanned"{
            //UnPlanned CallBack 
            if isCalled == true {
                print("UnPlanned Selected")
                self?.fetchUnPlannedData()
                self?.tableView.reloadData()
                selectedCheckBoxIndex = 1
            } else {
                self?.DeSelect()
                self?.tableView.reloadData()
                print("UnPlanned Not selected")
            }
            self?.checkBox_unPlannedisOn = isCalled
        }else{ 
            //Both CallBack 
            if isCalled == true { 
                print("Both Selected")
                self?.fetchDate()
                self?.tableView.reloadData()
                selectedCheckBoxIndex = 2
            } else {
                 self?.DeSelect()
                 self?.tableView.reloadData()
                print("both Not selected")
            }
             self?.checkBox_BothisOn = isCalled
        } 

    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download