带有复选框的交互式 Table 视图 Header
Interactive Table View Header with check boxes
我设计了 tableview 的 header,它通过更改图像的按钮制作的复选框进行交互。当我单击一个复选框时,它会被正确选中和取消选中。
我想要的是当我单击复选框时,如果它被选中,则两个复选框的其余部分应该自动取消选中。下面是代码。
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
}
这是习俗 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)
}
}
}
声明一个变量以跟踪选中的复选框并根据该变量的状态更新其值。
里面func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
修改代码如
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
}
}
我设计了 tableview 的 header,它通过更改图像的按钮制作的复选框进行交互。当我单击一个复选框时,它会被正确选中和取消选中。
我想要的是当我单击复选框时,如果它被选中,则两个复选框的其余部分应该自动取消选中。下面是代码。
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
}
这是习俗 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)
}
}
}
声明一个变量以跟踪选中的复选框并根据该变量的状态更新其值。
里面func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
修改代码如
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
}
}