我有一个UITableView设置自定义节头。当我点击section header时,它将折叠/展开。当向下滚动
我的问题是当我滚动一个部分到50%,点击部分标题崩溃,当前部分标题将消失,它被下一个部分标题所取代,用户必须向上滚动才能看到上一节。这是很糟糕的用户体验有什么办法解决这个问题吗?非常感谢
演示:https://freeimage.host/i/HfyjRDv我的代码:https://codefile.io/f/P3yiUQKkCB2L0VW13n9N
var hiddenSections = Set<Int>()
let tableViewData = [
["1","2","3","4","5", "6", "7", "8", "9", "10"],
["1","2","3","4","5", "6", "7", "8", "9", "10"],
["1","2","3","4","5", "6", "7", "8", "9", "10"],
["1","2","3","4","5", "6", "7", "8", "9", "10"],
["1","2","3","4","5", "6", "7", "8", "9", "10"],
]
func numberOfSections(in tableView: UITableView) -> Int {
return self.tableViewData.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.hiddenSections.contains(section) {
return 0
}
return self.tableViewData[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = self.tableViewData[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionButton = UIButton()
sectionButton.setTitle(String(section),
for: .normal)
sectionButton.backgroundColor = .systemBlue
sectionButton.tag = section
sectionButton.addTarget(self,
action: #selector(self.hideSection(sender:)),
for: .touchUpInside)
return sectionButton
}
@objc
private func hideSection(sender: UIButton) {
let section = sender.tag
func indexPathsForSection() -> [IndexPath] {
var indexPaths = [IndexPath]()
for row in 0..<self.tableViewData[section].count {
indexPaths.append(IndexPath(row: row,
section: section))
}
return indexPaths
}
self.tableView.beginUpdates()
if self.hiddenSections.contains(section) {
self.hiddenSections.remove(section)
self.tableView.insertRows(at: indexPathsForSection(),
with: .fade)
} else {
self.hiddenSections.insert(section)
self.tableView.deleteRows(at: indexPathsForSection(),
with: .fade)
}
self.tableView.endUpdates()
}
目前,我已经尝试使用"tableView.scrollTo"但这不是个好主意
预期:
当前段头在折叠时不消失
在这里输入图像描述[1]: https://i.stack.imgur.com/IDjyP.gif
专业地做
public struct Xection {
let rows: [XRow]
var collapsed: Bool
public init(rows: [XRow], collapsed: Bool = false) {
self.rows = rows
self.collapsed = collapsed
}
static func all() -> [Xection] {
let s = [Xection(rows: [XRow(name: "1", icon: ""),
XRow(name: "2", icon: ""),
XRow(name: "3", icon: "")]),
Xection(rows: [XRow(name: "5", icon: ""),
XRow(name: "11", icon: ""),
XRow(name: "zz", icon: "")]),
Xection(rows: [XRow(name: "zz", icon: ""),
XRow(name: "z", icon: ""),
XRow(name: "z", icon: "")])]
return s
}
}
public struct XRow {
var name: String
var icon:String
public init(name: String,icon:String) {
self.name = name
self.icon = icon
}
}
然后在你的班级
var sections = Xection.all()
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].collapsed ? 0 : sections[section].rows.count
}
// Cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = self.sections[indexPath.section].rows[indexPath.row].name
return cell
}
// Header
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionButton = UIButton()
sectionButton.setTitle(String(section),
for: .normal)
sectionButton.backgroundColor = .systemBlue
sectionButton.tag = section
sectionButton.addTarget(self,
action: #selector(self.hideSection(sender:)),
for: .touchUpInside)
return sectionButton
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 85.0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 85.0
}
@objc
private func hideSection(sender: UIButton) {
sections[sender.tag].collapsed = !sections[sender.tag].collapsed
tableView.reloadSections(NSIndexSet(index: sender.tag) as IndexSet, with: .automatic)
}