Ios 如何在点击其他分区时折叠打开的TableView分区
我使用的是可折叠的TableView部分,它工作正常,一切正常。但当其他未打开的部分点击时,我需要折叠已经打开的部分Ios 如何在点击其他分区时折叠打开的TableView分区,ios,swift,Ios,Swift,我使用的是可折叠的TableView部分,它工作正常,一切正常。但当其他未打开的部分点击时,我需要折叠已经打开的部分 struct CustomCell { var opened = Bool() var title = String() var sectionData = [String]() } // I'm using this part inside TableView didSelectForRowAt if tableViewData[in
struct CustomCell {
var opened = Bool()
var title = String()
var sectionData = [String]()
}
// I'm using this part inside TableView didSelectForRowAt
if tableViewData[indexPath.section].opened {
tableViewData[indexPath.section].opened = false
let section = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(section, with: .none)
} else {
tableViewData[indexPath.section].opened = true
let section = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(section, with: .none)
}
这是我项目的代码
@objc private func sectionTapped(recognizer:UITapGestureRecognizer) {
print("Tapped",recognizer.view?.tag)
guard let tag = recognizer.view?.tag else {
return
}
// We have already open section
if let currentExpandedTag = self.sectionExpanded {
//collapse
if tag == currentExpandedTag {
self.tableView.beginUpdates()
self.sectionExpanded = nil
self.tableView.reloadSections(IndexSet(integer: tag), with: .fade)
self.tableView.endUpdates()
} else {
// Collapse current and expand other
self.tableView.beginUpdates()
self.tableView.reloadSections(IndexSet(integer: currentExpandedTag), with: .fade)
self.sectionExpanded = tag
self.tableView.reloadSections(IndexSet(integer: tag), with: .fade)
self.tableView.endUpdates()
self.tableView.scrollToRow(at: IndexPath(row: 0, section: tag), at: .top, animated: true)
}
} else {
// Nothing expanded
self.tableView.beginUpdates()
self.sectionExpanded = tag
self.tableView.reloadSections(IndexSet(integer: tag), with: .fade)
self.tableView.endUpdates()
}
}
说明:
1) 在func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?
方法中,我已将标记设置为内容标题视图,并添加了点击手势
2) 我在视图controllervar sectionExpanded:Int?
中有一个属性,它跟踪扩展的节
3) 其他代码是不言自明的
4) 在func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
方法中,我检查了以下内容
if let sectionExpanded = self.sectionExpanded, section == sectionExpanded {
return subItemCount // From your array
}
return 0