展开表格视图部分时如何旋转图像?

时间:2018-07-18 11:53:47

标签: ios swift uitableview sections

当表格视图部分为imagecollapse时,我想旋转expanded。我厌倦了所有方法,但是没有用。我应该写什么方法或逻辑来使事情起作用?

import UIKit

struct cellData {

    var bool = Bool()
    var title = String()
    var sectionData = [String]()

}


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

   @IBOutlet weak var tableView: UITableView!

   //creating a struct for populating structure data

   var tableData = [cellData]()
   var selectedsection:Int!

    override func viewDidLoad() {

        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self

        tableData = [cellData(bool: false, title: "Man", sectionData: ["Cell1","Cell2","Cell3","Cell4"]),
                     cellData(bool: false, title: "Old", sectionData: ["Cell1","Cell2","Cell3","Cell4"]),
                     cellData(bool: false, title: "Women", sectionData: ["Cell1","Cell2","Cell3","Cell4"]),
        cellData(bool: false, title: "Girl", sectionData: ["Cell1","Cell2","Cell3","Cell4"]),
        cellData(bool: false, title: "Boy", sectionData: ["Cell1","Cell2","Cell3","Cell4"])                        
                    ]
        // Do any additional setup after loading the view, typically from a nib.
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return tableData.count
    }

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

        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 44)
        let image = UIImageView()
        image.frame = CGRect(x: tableView.frame.width - 20 , y: 0, width: 20, height: 20)
        image.image = UIImage.init(named: "triangle.png")


        let button = UIButton(type: .system)
        button.frame = CGRect(x: 0, y: 0, width: tableView.frame.width - 30, height: 50)
        button.setTitle("Open", for: .normal)
        button.backgroundColor = .yellow
        button.tag = section
        image.tag = button.tag
        button.addTarget(self, action: #selector(buttonclick), for: .touchUpInside)
        view.addSubview(image)
        view.addSubview(button)
        return view
    }

    @objc func buttonclick(sender: UIButton){

        selectedsection = sender.tag
        //collapse
        if tableData[sender.tag].bool == true{   
            tableData[sender.tag].bool = false
            let section = IndexSet.init(integer: sender.tag)

            tableView.reloadSections(section, with: .fade)
        }
       //expanding
       else{
            tableData[sender.tag].bool = true
            let section = IndexSet.init(integer: sender.tag)

            tableView.reloadSections(section, with: .automatic)
        }
    }


    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableData[section].bool == true{
           return tableData[section].sectionData.count
        }
        else{
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        print("section\(indexPath.section) row\(indexPath.row) ")
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableViewCell
        cell?.label.text = tableData[indexPath.section].sectionData[indexPath.row]
        return cell!
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试给按钮添加标签,然后执行此操作,因为标签是默认值,且标签应大于0

c1 <- seq(1, 4)
c2 <- seq(5, 8)

library(Matrix)
bandSparse(5, 4, 0:-1, list(c1, c2))
#5 x 4 sparse Matrix of class "dgCMatrix"
#            
#[1,] 1 . . .
#[2,] 5 2 . .
#[3,] . 6 3 .
#[4,] . . 7 4
#[5,] . . . 8