2017-07-07 87 views
0

我有下面的代碼,它將標題添加到tableView中的4列。 我想根據用戶點擊哪個標頭進行排序,這是否可以用我創建列標題的方式進行?viewForHeaderInSection中的可選標題swift 3.1

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let v = UIView() 
     let segmentedControl = UISegmentedControl(frame: CGRect(x:3, y: 5, width: tableView.frame.width-6, height: 30)) 
     v.addSubview(segmentedControl) 
     v.backgroundColor = UIColor.white 
     segmentedControl.insertSegment(withTitle: "Name", at: 0, animated: false) 
     segmentedControl.insertSegment(withTitle: "Points", at: 1, animated: false) 
     segmentedControl.insertSegment(withTitle: "Played", at: 2, animated: false) 
     segmentedControl.insertSegment(withTitle: "Ave", at: 3, animated: false) 

     return v 
    } 

回答

0

我添加按鈕頭部像這樣:

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() 
}