indexPath содержит строку и раздел, так что вы можете легко определить, какой раздел выбран, если массив содержит indexPath, а затем удалить объект, я работаю над компанией
т всем, у меня есть многосекционный дизайн табличного представления, как
Я хочу применить фильтр на основе выбранных строк, в которых весь выбор находится в состоянии ИЛИ, кроме раздела критериев поиска (этот раздел является обязательным), теперь я хочу добиться того, чтобы сохранить все выбранные элементы и отфильтровать данные, когда пользователь нажимает на Применить кнопку. Как добиться этого. Пожалуйста, помогите.
примечание: я не хочу использовать любую базу данных, потому что в будущем я должен опубликовать все выбранные значения, используя метод 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.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
}
}
Пожалуйста, помогите. Я много гуглил, но не могу найти суть моего сценария.