[英]Multi selection table view - Rotate image in custom header when section expands/collapses
[英]How to rotate Image while expanding section of Table View?
我想在tableview部分collapse
和expanded
时旋转image
。 我厌倦了所有方法,但是没有用。 我应该写什么方法或逻辑来使事情起作用?
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!
}
}
您可以尝试给按钮添加一个标签并执行此操作,以默认值大于0的标签开始
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
image.tag = section + 1
button.tag = section
button.addTarget(self, action: #selector(buttonclick), for: .touchUpInside)
view.addSubview(image)
view.addSubview(button)
return view
}
@objc func buttonclick(sender: UIButton){
let imageV = sender.superView!.viewWithTag(sender.tag + 1) as! UIImageView
UIView.animate(withDuration: 0.5) {
imageV.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
}
if tableData[sender.tag].bool == true{//collapse
tableData[sender.tag].bool = false
let section = IndexSet.init(integer: sender.tag)
tableView.reloadSections(section, with: .fade)
}
else{//expanding
tableData[sender.tag].bool = true
let section = IndexSet.init(integer: sender.tag)
tableView.reloadSections(section, with: .automatic)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.