2017-06-27 153 views
0

我正在瀏覽試圖在我的應用程序中構建可擴展列表視圖的這個教程(https://github.com/jeantimex/ios-swift-collapsible-table-section)。我的代碼和教程之間唯一的區別是我正在用故事板做。可擴展列表視圖不會在swift中摺疊

我似乎有它幾乎完全功能,但我有一個問題。行正在崩潰,然後重新填充而不會再次點擊。我多次瀏覽了我的代碼,但似乎無法找到問題所在。

這裏是我的代碼:

列表視圖控制器類:

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 
} 
} 

那麼爲什麼我的細胞在我試圖崩潰時會重新生長呢?任何幫助表示讚賞。謝謝!

回答

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 
}