我添加按鈕頭部像這樣:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
let segmentedControl = UISegmentedControl(frame: CGRect(x:0, y: 5, width: tableView.frame.width-6, height: 40))
v.addSubview(segmentedControl)
v.backgroundColor = UIColor.white
let btnName: UIButton = UIButton(frame: CGRect(x: 5, y: 5, width: 120, height: 30))
btnName.backgroundColor = UIColor.white
btnName.showsTouchWhenHighlighted = true
btnName.layer.borderWidth = 1
btnName.layer.borderColor = UIColor.lightGray.cgColor
btnName.setTitle("Name", for: .normal)
btnName.setTitleColor(UIColor.blue, for: .normal)
btnName.setTitleColor(UIColor.red, for: .selected)
btnName.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnName.tag = 1
v.addSubview(btnName)
let btnPoints: UIButton = UIButton(frame: CGRect(x: 125, y: 5, width: 80, height: 30))
btnPoints.backgroundColor = UIColor.white
btnPoints.showsTouchWhenHighlighted = true
btnPoints.layer.borderWidth = 1
btnPoints.layer.borderColor = UIColor.lightGray.cgColor
btnPoints.setTitle("Points", for: .normal)
btnPoints.setTitleColor(UIColor.blue, for: .normal)
btnPoints.setTitleColor(UIColor.red, for: .selected)
btnPoints.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnPoints.tag = 2
v.addSubview(btnPoints)
let btnPlayed: UIButton = UIButton(frame: CGRect(x: 205, y: 5, width: 80, height: 30))
btnPlayed.backgroundColor = UIColor.white
btnPlayed.showsTouchWhenHighlighted = true
btnPlayed.layer.borderWidth = 1
btnPlayed.layer.borderColor = UIColor.lightGray.cgColor
btnPlayed.setTitle("Played", for: .normal)
btnPlayed.setTitleColor(UIColor.blue, for: .normal)
btnPlayed.setTitleColor(UIColor.red, for: .focused)
btnPlayed.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnPlayed.tag = 4
v.addSubview(btnPlayed)
let btnAve: UIButton = UIButton(frame: CGRect(x: 285, y: 5, width: 80, height: 30))
btnAve.backgroundColor = UIColor.white
btnAve.showsTouchWhenHighlighted = true
btnAve.layer.borderWidth = 1
btnAve.layer.borderColor = UIColor.lightGray.cgColor
btnAve.setTitle("Ave", for: .normal)
btnAve.setTitleColor(UIColor.blue, for: .normal)
btnAve.setTitleColor(UIColor.red, for: .highlighted)
btnAve.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnAve.tag = 3
v.addSubview(btnAve)
return v
}
func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
btnNamePressed = !btnNamePressed
if btnNamePressed == true
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Name < $1.Name})
}
else
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Name > $1.Name})
}
}
if btnsendtag.tag == 2 {
btnPointsPressed = !btnPointsPressed
if btnPointsPressed == true
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Points < $1.Points})
}
else
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Points > $1.Points})
}
}
if btnsendtag.tag == 3 {
btnPlayedPressed = !btnPlayedPressed
if btnPlayedPressed == true
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Played < $1.Played})
}
else
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Played > $1.Played})
}
}
if btnsendtag.tag == 4 {
btnAvePressed = !btnAvePressed
if btnAvePressed == true
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Average < $1.Average})
}
else
{
GlobalVar.listOf = GlobalVar.listOf.sorted(by: {$0.Average > $1.Average})
}
}
tableView.reloadData()
}