gpt4 book ai didi

ios - 折叠可折叠部分标题时下拉 UITableView 表标题

转载 作者:行者123 更新时间:2023-11-28 05:47:19 28 4
gpt4 key购买 nike

我有一个基于另一个堆栈溢出帖子的 uitableview 部分的可折叠标题(不知道现在在哪里,因为那是几个月前)。碰巧的是,测试人员发现了一个奇怪的错误,即折叠所有部分会拉下表格标题 View 。

*编辑 TableView 标题只是一个 UI View ,我将其放入 Storyboard 中,在 TableView 内,在原型(prototype)单元格上方。没有重大限制。只是单元格和标题的高度。 tableview 被固定到安全区域。

一切看起来都很好,直到您将其中一个部分展开到屏幕外,然后将其向上滚动,以便行开始在顶部的 float 部分标题下滑动。然后你点击折叠它。它崩溃了,但标题 View 被拉下了。它看起来像是当部分适合一个屏幕时发生的,并且行在折叠前稍微滚动。

如有任何帮助,我们将不胜感激。

在我的demo项目中(乐于分享),当四个section收起时,是这样的: enter image description here

当用户展开某些部分并滚动时,部分标题会粘在顶部,内容会滚动到其下方,然后折叠粘性部分标题,它可能如下所示: enter image description here

我有一个委托(delegate)协议(protocol):

protocol CollapsibleHeaderViewDelegate: class {
func toggleSection(header: CollapsibleSectionHeader, section: Int)
}

protocol SectionHeaderCollapsible {
var isCollapsed: Bool { get }
var rowCount: Int { get }
}

以及 UITableVieHeaderFooterView 的子类:

class CollapsibleHeader: UITableViewHeaderFooterView {

@IBOutlet var sectionHeaderLabel: UILabel!
var collapsed = false
weak var delegate: CollapsibleHeaderViewDelegate?
var sectionItem: SectionHeaderCollapsible?

static let reuseIdentifer = "CollapsibleHeader"

func configure(headerText: String) {
textLabel?.text = headerText
}

override func awakeFromNib() {
super.awakeFromNib()
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapHeader)))
}

@objc private func didTapHeader(gestureRecognizer: UITapGestureRecognizer) {
guard let header = gestureRecognizer.view as? CollapsibleHeader else { return }

delegate?.toggleSection(header: self, section: header.tag)
}
}

然后代表做类似的事情。这个:

struct CollapsibleSection: SectionHeaderCollapsible {
var isCollapsed: Bool = false
var rowCount: Int {
get {
return isCollapsed ? 0 : dataContents.count
}
}
var dataContents: [String]
}

class ViewController: UIViewController {

@IBOutlet var tableView: UITableView!
@IBOutlet var headerView: UITableView!

var sections = [CollapsibleSection(isCollapsed: false, dataContents: ["first", "second"]),
CollapsibleSection(isCollapsed: false, dataContents: ["red", "blue"]),
CollapsibleSection(isCollapsed: false, dataContents: ["seven", "five"]),
CollapsibleSection(isCollapsed: false, dataContents: ["Josephine", "Edward"])]

override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let nib = UINib(nibName: "CollapsibleHeader", bundle: nil)
tableView.register(nib, forHeaderFooterViewReuseIdentifier: "CollapsibleHeader")
}
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].rowCount
}

func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") else { fatalError() }
cell.textLabel?.text = sections[indexPath.section].dataContents[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "CollapsibleHeader") as? CollapsibleHeader else { fatalError() }
header.sectionHeaderLabel.text = "Section \(section + 1)"
header.delegate = self
header.tag = section
return header
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}
}

extension ViewController: CollapsibleHeaderViewDelegate {
func toggleSection(header: CollapsibleHeader, section: Int) {
sections[section].isCollapsed = !sections[section].isCollapsed
tableView.reloadSections([section], with: .fade)
}
}

编辑:看起来我的同事根据(或至少类似于)您的回答创建了一个解决方法:

if tableView.contentOffset.y < 0 { 
var offset = tableView.contentOffset
offset.y = tableView.contentSize.height - tableView.bounds.height
tableView.setContentOffset(offset, animated: true)
} else {
tableView.setContentOffset(tableView.contentOffset, animated: true)
}

最佳答案

遇到同样的问题,显然就在“reloadSections”之后,tableView 的 contentOffset.y 有一些奇怪的值(你可以在“reloadSections”之前和之后打印“tableView.contentOffset.y”时看到它)。所以我只是在 contentOffset 展开后设置为 0 偏移值:

    let offset = tableView.contentOffset.y
// Reload section
tableView.reloadSections(IndexSet(integer: section), with: .automatic)

if !sections[section].isCollapsed {
tableView.contentOffset.y = offset - offset
}

关于ios - 折叠可折叠部分标题时下拉 UITableView 表标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54192294/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com