Hello all m having multisectioned tableview design like
want to apply filter based on the selected rows in which all the selection is in OR condition except the searchcriteria section(this section is mandatory),now what i want to achieve is want to save all the selection and filter the data when user clicks on Apply button.How to achieve this.Please help.
note:i dont want to use any database coz in future i have to post all the selected values using POST method.
Code :
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.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
}
}
Please Help.I googled a lot but cant find the soultion of my scenario.
See Question&Answers more detail:
os