我正在阅读本教程(https://github.com/jeantimex/ios-swift-collapsible-table-section),尝试在我的应用程序中构建可扩展的列表视图。我的代码和教程之间唯一的区别就是我正在使用故事板。
我似乎几乎完全正常运作,但我有一个问题。行正在折叠,然后重新填充而不会再次单击。我已多次浏览我的代码但似乎无法找到问题。
这是我的代码:
列表视图控制器类:
import UIKit
class ItemListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var sections = [Item]()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
sections = [
Item(name: "Mac", items: ["MacBook", "MacBook Air", "MacBook Pro", "iMac", "Mac Pro", "Mac mini", "Accessories", "OS X El Capitan"]),
Item(name: "iPad", items: ["iPad Pro", "iPad Air 2", "iPad mini 4", "Accessories"]),
Item(name: "iPhone", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"])
]
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].items.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "ItemTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ItemTableViewCell else {
fatalError("The dequeued cell is not an instance of ItemTableViewCell.")
}
cell.itemLabel.text = sections[indexPath.section].items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCellIdentifier = "ItemHeaderTableViewCell"
let headerCell = tableView.dequeueReusableCell(withIdentifier: headerCellIdentifier) as! ItemHeaderTableViewCell
headerCell.itemHeaderLabel.text = sections[section].name
headerCell.setCollapsed(sections[section].collapsed)
headerCell.section = section
headerCell.delegate = self
return headerCell
}
}
extension ItemListViewController: ItemHeaderTableViewCellDelegate {
func toggleSection(_ header: ItemHeaderTableViewCell, section: Int) {
let collapsed = !sections[section].collapsed
// Toggle collapse
sections[section].collapsed = collapsed
header.setCollapsed(collapsed)
// Adjust the height of the rows inside the section
tableView.beginUpdates()
for i in 0 ..< sections[section].items.count {
let indexPath = IndexPath(item: i, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)
}
tableView.endUpdates()
}
}
项目标题表格查看单元格类:
import UIKit
protocol ItemHeaderTableViewCellDelegate {
func toggleSection(_ header: ItemHeaderTableViewCell, section: Int)
}
class ItemHeaderTableViewCell: UITableViewCell {
var delegate: ItemHeaderTableViewCellDelegate?
var section: Int = 0
@IBOutlet var itemHeaderLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ItemHeaderTableViewCell.tapHeader(_:))))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func tapHeader(_ gestureRecognizer: UITapGestureRecognizer) {
guard let cell = gestureRecognizer.view as? ItemHeaderTableViewCell else {
return
}
delegate?.toggleSection(self, section: cell.section)
}
func setCollapsed(_ collapsed: Bool) {
// Animate the arrow rotation (see Extensions.swf)
// arrowLabel.rotate(collapsed ? 0.0 : CGFloat(M_PI_2))
}
}
项目表格查看单元格:
import UIKit
class ItemTableViewCell: UITableViewCell {
@IBOutlet var itemLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
项目类:
import UIKit
class Item {
var name: String!
var items: [String]!
var collapsed: Bool!
init(name: String, items: [String], collapsed: Bool = false) {
self.name = name
self.items = items
self.collapsed = collapsed
}
}
那么当我试图折叠它们时,为什么我的细胞会重新填充呢?任何帮助表示赞赏。谢谢!
答案 0 :(得分:1)
看起来你错过了实际显示/隐藏行的功能:
// from the GitHub repo you linked to:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return sections[(indexPath as NSIndexPath).section].collapsed! ? 0 : 44.0
}
答案 1 :(得分:1)
如果您正在使用storyboard,我有一个可扩展UITableView的存储库,您可以查看它,如果它适合您。