如何使用swift 3在UITableView中基于多个选定的行应用过滤器

时间:2017-08-26 05:57:20

标签: uitableview swift3 filtering multipleselection uitableviewsectionheader

大家好,我有多层次的tableview设计,如belowimage

想要根据所有选择处于OR状态的选定行应用过滤器,但searchcriteria部分除外(此部分是必需的),现在我想要实现的是想要保存所有选择并过滤数据时用户单击“应用”按钮。如何实现此目的。请帮助。

注意:我以后不想使用任何数据库因为我必须使用POST方法发布所有选定的值。

代码:

ViewController.swift

import UIKit

class ViewController: UIViewController,ExpandableHeaderViewDelegate,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tableview: UITableView!

var sections =     [

Section(sectionname: "Year" ,
        cellnames: ["2017-2018","2016-2017","2015-2016"],
        expanded: false,
        subtitle: "Please select a Year"),

Section(sectionname: "School",
        cellnames: ["School A","School B","School C"],
        expanded: false,
        subtitle: "Please select a School"),

Section(sectionname: "SearchCriteria",
        cellnames: ["No of Students","No of Student on RTE","No of Differently Abled Students","No of Student Opted For School Transport"],
        expanded: false,
        subtitle: "Please select your SearchCriteria"),

Section(sectionname: "Class",
        cellnames: ["IX","X","XI","XII"],
        expanded: false,
        subtitle: "Please select a Class"),

Section(sectionname: "Section",
        cellnames: ["A","B","C","D","E"],
        expanded: false,
        subtitle: "Please Select a Section")

]

var selectIndexPath : IndexPath!
override func viewDidLoad() {
    super.viewDidLoad()

    self.tableview.allowsMultipleSelection = true
    selectIndexPath = IndexPath(row: -1, section: -1)
    let nib = UINib(nibName: "ExpandableHeaderView", bundle: nil)
    tableview.register(nib, forHeaderFooterViewReuseIdentifier: "expandableHeaderView")
    // Do any additional setup after loading the view, typically from a nib.
}

func numberOfSections(in tableView: UITableView) -> Int {

    return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].cellnames.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (sections[indexPath.section].expanded)
    {
        return 44
    }
    else
    {
        return 0
    }
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 2
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerview = tableview.dequeueReusableHeaderFooterView(withIdentifier: "expandableHeaderView") as! ExpandableHeaderView
    headerview.customInit(title: sections[section].sectionname, subtitle: sections[section].subtitle, section: section, delegate: self)
    return headerview

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "labelcell")
    cell?.textLabel?.text = sections[indexPath.section].cellnames[indexPath.row]
    cell?.accessoryType = (indexPath == selectIndexPath) ? .checkmark : .none
    return cell!

}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectIndexPath = indexPath
    self.sections[indexPath.section].subtitle = (tableview.cellForRow(at: indexPath)?.textLabel?.text)!
    sections[indexPath.section].expanded = !sections[indexPath.section].expanded
    tableview.beginUpdates()
    tableview.reloadSections([indexPath.section], with: .automatic)
    tableview.endUpdates()

}
func toggleSection(header:ExpandableHeaderView,section : Int)
{
    sections[section].expanded = !sections[section].expanded
    tableview.beginUpdates()
    for i in 0 ..< sections[section].cellnames.count {
        tableview.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
    }
    tableview.endUpdates()
}



@IBAction func done_action(_ sender: Any) {

}


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


}

ExpandableHeaderView.xib ExpandableHeaderView.xib

ExpandableHeaderView.swift

 import UIKit
 protocol ExpandableHeaderViewDelegate {
 func toggleSection(header:ExpandableHeaderView,section:Int)

}

class ExpandableHeaderView: UITableViewHeaderFooterView {

var delegate :ExpandableHeaderViewDelegate?
var section : Int!
@IBOutlet weak var TitleLabel: UILabel!

@IBOutlet weak var SubTitleLabel: UILabel!


override init(reuseIdentifier: String?) {
    super.init(reuseIdentifier: reuseIdentifier) 
    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderView)))

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder : aDecoder )

    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderView)))

}

func selectHeaderView(gesture:UITapGestureRecognizer)
{
    let cell = gesture.view as! ExpandableHeaderView
    delegate?.toggleSection(header: self, section: cell.section)
}
func customInit(title:String,subtitle : String,section:Int,delegate:ExpandableHeaderViewDelegate)
{
    self.TitleLabel.text = title
    self.SubTitleLabel.text = subtitle
    self.section = section
    self.delegate = delegate
}

override func layoutSubviews() {
    super.layoutSubviews()
    self.TitleLabel?.textColor = UIColor.white
    self.SubTitleLabel?.textColor = UIColor.white
    self.SubTitleLabel?.alpha = 0.7
    self.contentView.backgroundColor = UIColor.darkGray
}
 }

Section.swift

 import Foundation

  struct Section
 {
   var sectionname : String!
   var cellnames : [String]!
   var expanded : Bool!
   var subtitle : String!

   init(sectionname:String,cellnames : [String],expanded : Bool,subtitle : String)
  {
    self.sectionname = sectionname
    self.cellnames = cellnames
    self.expanded = expanded
    self.subtitle = subtitle
}
}

请帮助。我搜索了很多,但无法找到我的场景的灵魂。

0 个答案:

没有答案