Reputation: 21
I am implementing a side menu in my iOS app using UITableView. The table view displays a list of categories, where each category can have multiple sub-items. Each category is represented by a section, and clicking on a category expands or collapses the section to show or hide its sub-items. The issue I'm facing is that all sections, except one, are shown as expanded by default, even though their icons indicate they should be collapsed.
I would like all sections to be collapsed initially and only expand when the user clicks on the section header. Despite my attempts to manage this behavior through hiddenSections and expandedSections sets, I'm still encountering problems.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
bindViewModel()
expandedSections.removeAll()
hiddenSections = Set(0 ..< categories.count)
tableView.reloadData()
}
func bindViewModel() {
viewModel?.getSideMenu()
viewModel?.menuList.subscribe(onNext: { categories in
self.categories = categories
for (index, model) in categories.enumerated() {
for item in model.items {
switch item {
case .CategoriesSectionItem:
self.hiddenSections.insert(index)
default:
break
}
}
}
self.tableView.reloadData()
}).disposed(by: disposeBag)
}
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
if hiddenSections.contains(section) {
return 0
}
for item in categories[section].items {
switch item {
case let .CategoriesSectionItem(control):
return control.controls?.count ?? 0
default:
break
}
}
return 0
}
func itemClicked(sender: UIButton, lineView: UIView, icon: UIImageView, categoriesCount _: Int, selectedIndex _: Int?, subControl _: SideMenuControl?, control _: CategoriesModel?) {
let section = sender.tag
if hiddenSections.contains(section) {
hiddenSections.remove(section)
icon.image = UIImage(named: "MinusIcon")
lineView.isHidden = true
let indexPathsToInsert = indexPathsForSection(section)
if !indexPathsToInsert.isEmpty {
tableView.insertRows(at: indexPathsToInsert, with: .fade)
}
expandedSections.append(section)
} else {
hiddenSections.insert(section)
icon.image = UIImage(named: "PlusIcon")
let indexPathsToDelete = indexPathsForSection(section)
if !indexPathsToDelete.isEmpty {
tableView.deleteRows(at: indexPathsToDelete, with: .fade)
}
lineView.isHidden = false
}
for expandedSection in expandedSections where section != expandedSection {
if hiddenSections.contains(expandedSection) { continue }
hiddenSections.insert(expandedSection)
let indexPathsToDelete = indexPathsForSection(expandedSection)
if !indexPathsToDelete.isEmpty {
tableView.deleteRows(at: indexPathsToDelete, with: .fade)
}
tableView.reloadSections([expandedSection], with: .none)
if let index = expandedSections.firstIndex(of: expandedSection) {
expandedSections.remove(at: index)
}
}
}
func indexPathsForSection(_ section: Int) -> [IndexPath] {
var indexPaths = [IndexPath]()
for item in categories[section].items {
switch item {
case let .CategoriesSectionItem(control):
for row in 0 ..< (control.controls?.count ?? 0) {
indexPaths.append(IndexPath(row: row, section: section))
}
default:
break
}
}
return indexPaths
}
I am using a combination of hiddenSections and expandedSections to manage which sections are open or closed. Despite setting up these controls, all sections appear expanded by default, but the icons next to them show a plus sign, indicating they should be collapsed.
How can I ensure that all sections are collapsed initially, matching their icons, and only expand when a section header is clicked?
Upvotes: 0
Views: 36