viewForHeaderInSection中的可选标题swift 3.1

时间:2017-07-07 11:00:01

标签: swift3 tableview

我有以下代码,它将标题添加到tableView中的4列。 我想根据用户点击的标题进行排序,是否可以通过创建列标题的方式进行排序?

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
    }

1 个答案:

答案 0 :(得分: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()
}