3

こんにちは、マルチセクションのテーブルビュー設計を持つすべての m下の画像

searchcriteria セクション (このセクションは必須) を除いて、すべての選択が OR 条件にある選択された行に基づいてフィルターを適用したい、今私が達成したいのは、すべての選択を保存し、ユーザーがクリックしたときにデータをフィルターすることですボタンを適用します。これを達成する方法。助けてください。

注:今後データベースを使用したくないので、選択したすべての値を 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
}
}

助けてください.私はたくさんグーグルで検索しましたが、私のシナリオの魂を見つけることができません.

4

0 に答える 0