[ad_1]
I’ve a tableView with completely different subcategories (“Algrebra”,”Biology”,”Chemistry”) who’re listed and searchable through the searchController. I wish to put these subcategories inside a number of classes (“Pressing”,”Necessary”,”Not Necessary”) and develop/collapse them on click on. I additionally wish to have the classes listed (as an alternative of the subcategories) however hold the subcategories searchable through the searchController.
I do not know methods to implement it correctly with my code.
This is my code:
CategoryController
class CategoryController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating {
non-public var searchController = UISearchController()
let classes = ["Urgent", "Important", "Not Important"]
let subcategories = [
Add(category: "Algrebra", categoryImg: #imageLiteral(resourceName: "Algebra.png")),
Add(category: "Biology", categoryImg: #imageLiteral(resourceName: "Biology.png")),
Add(category: "Chemistry", categoryImg: #imageLiteral(resourceName: "Chemistry.png")),
]
non-public var sectionTitles = [String]()
non-public var filteredSectionTitles = [String]()
non-public var sortedCategory = [(key: String, value: [Add])]()
non-public var filteredCategory = [(key: String, value: [Add])]()
non-public let tableView: UITableView = {
let desk = UITableView()
desk.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return desk }()
override func viewDidLoad() {
tremendous.viewDidLoad()
//TABLEVIEW
tableView.rowHeight = 50
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
tableView.register(CategoryCell.self, forCellReuseIdentifier: "cell")
tableView.dataSource = self
tableView.delegate = self
tableView.sectionIndexColor = .black
tableView.sectionIndexBackgroundColor = .lightGray
tableView.sectionIndexTrackingBackgroundColor = .grey
tableView.allowsMultipleSelection = false
//SEARCHCONTROLLER
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Seek for your class"
self.searchController.hidesNavigationBarDuringPresentation = false
self.navigationItem.searchController = self.searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
self.navigationItem.title = "Duties"
navigationController?.navigationBar.prefersLargeTitles = true
self.searchController.searchBar.searchTextField.textColor = .label
let groupedList = Dictionary(grouping: self.subcategories, by: { String($0.class.prefix(1)) })
self.sortedCategory = groupedList.sorted{$0.key < $1.key}
for tuple in self.sortedCategory {
self.sectionTitles.append(tuple.key)
}
}
//VIEWDIDLAYOUT
override func viewDidLayoutSubviews() {
tremendous.viewDidLayoutSubviews()
tableView.body = view.bounds
}
/// TABLEVIEW
func tableView(_ tableView: UITableView, titleForHeaderInSection part: Int) -> String? {
if self.searchController.isActive && !self.filteredSectionTitles.isEmpty {
return self.filteredSectionTitles[section]
} else {
return self.sectionTitles[section]
}
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if self.searchController.isActive && !self.filteredSectionTitles.isEmpty {
return self.filteredSectionTitles
} else {
return self.sectionTitles
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if self.searchController.isActive && !self.filteredSectionTitles.isEmpty {
return self.filteredSectionTitles.rely
} else {
return self.sectionTitles.rely
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
if self.searchController.isActive && !self.filteredCategory.isEmpty {
return self.filteredCategory[section].worth.rely
} else {
return self.sortedCategory[section].worth.rely
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath) as UITableViewCell
cell.imageView?.contentMode = .scaleAspectFit
if self.searchController.isActive && !self.filteredCategory.isEmpty {
cell.textLabel?.textual content = self.filteredCategory[indexPath.section].worth[indexPath.row].class
cell.imageView?.picture = self.filteredCategory[indexPath.section].worth[indexPath.row].categoryImg
} else {
cell.textLabel?.textual content = self.sortedCategory[indexPath.section].worth[indexPath.row].class
cell.imageView?.picture = self.sortedCategory[indexPath.section].worth[indexPath.row].categoryImg
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
Add.particulars.class = (currentCell.textLabel?.textual content)!
let secondVC = DateController()
navigationController?.pushViewController(secondVC, animated: true)
print(Add.particulars.class)
}
func updateSearchResults(for searchController: UISearchController) {
guard let textual content = self.searchController.searchBar.textual content else {
return
}
let filteredCategory = self.sortedCategory.flatMap { $0.worth.filter { $0.class.comprises(textual content) } }
let groupedCategory = Dictionary(grouping: filteredCategory, by: { String($0.class.prefix(1)) } )
self.filteredCategory = []
self.filteredCategory = groupedCategory.sorted{ $0.key < $1.key }
self.filteredSectionTitles = []
for tuple in self.filteredCategory {
self.filteredSectionTitles.append(tuple.key)
}
self.tableView.reloadData()
}
}
CategoryCell
class CategoryCell: UITableViewCell {
var cellImageView = UIImageView()
var cellLabel = UILabel()
override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
tremendous.init(model: model, reuseIdentifier: "cell")
cellImageView.translatesAutoresizingMaskIntoConstraints = false
cellImageView.contentMode = .scaleAspectFit
cellImageView.tintColor = .systemPink
contentView.addSubview(cellImageView)
cellLabel.translatesAutoresizingMaskIntoConstraints = false
cellLabel.font = UIFont.systemFont(ofSize: 20)
contentView.addSubview(cellLabel)
NSLayoutConstraint.activate([
cellImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
cellImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
cellImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
cellImageView.widthAnchor.constraint(equalToConstant: 44),
cellLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
cellLabel.leadingAnchor.constraint(equalTo: cellImageView.trailingAnchor, constant: 10),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been applied")
}
override func setSelected(_ chosen: Bool, animated: Bool) {
tremendous.setSelected(chosen, animated: animated)
}
}
Add(DataStruct)
struct Add {
static var particulars: Add = Add()
var class: String = ""
func getDict() -> [String: Any] {
let dict = ["category": self.category,
] as [String : Any]
return dict
}
}
[ad_2]