Ios 可展开的listview未在swift中折叠
我正在学习本教程()试图在我的应用程序中构建一个可扩展的listview。我的代码和教程之间的唯一区别是我使用的是故事板 我似乎有它几乎完全功能,但我有一个问题。行正在折叠,然后在没有再次单击的情况下重新填充。我已经检查了我的代码多次,但我似乎找不到问题 这是我的密码: 列表视图控制器类:Ios 可展开的listview未在swift中折叠,ios,swift,listview,Ios,Swift,Listview,我正在学习本教程()试图在我的应用程序中构建一个可扩展的listview。我的代码和教程之间的唯一区别是我使用的是故事板 我似乎有它几乎完全功能,但我有一个问题。行正在折叠,然后在没有再次单击的情况下重新填充。我已经检查了我的代码多次,但我似乎找不到问题 这是我的密码: 列表视图控制器类: import UIKit class ItemListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
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
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
}
}
那么,为什么我的细胞在我试图使它们崩溃时会重新繁殖呢?感谢您的帮助。谢谢 看起来您缺少了实际显示/隐藏行的函数:
// 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
}
看起来您缺少了实际显示/隐藏行的函数:
// 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
}
如果您使用的是故事板,我有一个可扩展UITableView的存储库,如果它适合您,您可以查看它
如果您使用的是故事板,我有一个可扩展UITableView的存储库,如果它适合您,您可以查看它