我使用斯威夫特3.斯威夫特3 - 擴展表視圖細胞,而不關閉其他的
我一直在關注這個教程:https://www.youtube.com/watch?v=VWgr_wNtGPM,通過this answer在計算器上補充。
但是,這樣做的方式是,如果我點擊一個單元格,它會在隱藏其他單元格的同時展開。我如何做到這一點,當我擴展它時,其他已擴展的單元格保持擴展?
我使用斯威夫特3.斯威夫特3 - 擴展表視圖細胞,而不關閉其他的
我一直在關注這個教程:https://www.youtube.com/watch?v=VWgr_wNtGPM,通過this answer在計算器上補充。
但是,這樣做的方式是,如果我點擊一個單元格,它會在隱藏其他單元格的同時展開。我如何做到這一點,當我擴展它時,其他已擴展的單元格保持擴展?
我建議您以優雅的方式實現此目標的最佳方法是通過UIStackView
元素實現它。
看看這個帖子http://www.atomicbird.com/blog/uistackview-table-cells
,如果你想自己做,你可以試試這個方法。
第一步是你應該創建一個模型列表就像:
var cellsData: [CustomData] = [];
的CustomData
看起來像:
class CustomData {
var isExpanded: Bool = false;
// whatever other variables
}
那麼你的自定義單元格應該什麼樣子,但你必須做的事tableView:didSelectItemAt
like:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = indexPath.row;
self.cellsData[row].isExpanded = !self.cellsData[row].isExpanded;
self.tableView.reloadRows(at: [indexPath], with: .none); // or the other animations
}
然後在「tableView:cell ForRowAt」似乎是:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell;
if(cell.isExpanded){
// do something when the cell is expanded
}else{
// do something when the cell is not expanded
}
}
記得,細胞是可重複使用的,如果你已經使用了電池一次以上,則該單元將保持狀態,當它被上次使用的時間意味着。
您可以使用ExpyTableView,它會從給定的標題單元格中生成可展開的部分。與iOS 8.0兼容。
所有您需要做的是import ExpyTableView
,然後:
class ViewController: ExpyTableViewDataSource, ExpyTableViewDelegate {
@IBOutlet weak var expandableTableView: ExpyTableView!
// First, set data source and delegate for your table view.
override func viewDidLoad() {
super.viewDidLoad()
expandableTableView.dataSource = self
expandableTableView.delegate = self
}
// Then return your expandable cell instance from expandingCell data source method.
func expandableCell(forSection section: Int, inTableView tableView: ExpyTableView) -> UITableViewCell {
// this cell will be displayed at IndexPath with section: section and row 0
}
}
你可以看到現在你的舊錶視圖部分是一個可擴展表格視圖部分。您也可以下載example project並查看更詳細的示例。
你已經解決您的疑問?如果不是,那麼我會幫助你。 –