我有一个带有XIB-customCell的UITableView,其中包含2个视图:
当我按下 fooView按钮时,我想在 fooView
下显示/隐藏 mapView当我按下 fooView按钮切换 mapView 时,我需要多次按下它以展开/折叠单元格。
为了扩展单元格,我使用以下代码:
fileprivate func toggleMap() {
if state == .defaultState {
mapHeightConstraint.priority = UILayoutPriority(999)
state = .mapState
}
else {
mapHeightConstraint.priority = UILayoutPriority(250)
state = .defaultState
}
delegate.updateTable(index)
}
委托是包含tableView(DashboardViewController)的ViewController
updateTable(index)是ViewController的功能
func updateTable(_ index: IndexPath) {
tableView.beginUpdates()
tableView.reloadRows(at: [index], with: .automatic)
tableView.endUpdates()
}
这是我的customCell
import UIKit
import MapKit
@IBDesignable
class TripCell: UITableViewCell {
// MARK: - Cell State
enum CellState {
case defaultState
case mapState
}
// MARK: - Delegate Stuff for reloading
var state: CellState = .defaultState
var delegate: DashboardViewController!
var index: IndexPath!
// MARK: - IBOutlets
@IBOutlet weak var mapState: UIView!
// mapStateHeightConstraint: (constant: 0, priority 750)
@IBOutlet weak var mapStateHeightConstraint: NSLayoutConstraint!
// mapHeightConstraint: (constant: 100, priority: 250)
@IBOutlet weak var mapHeightConstraint: NSLayoutConstraint!
// Label
@IBOutlet weak var origin: UILabel!
// Button
@IBOutlet weak var showOnMap: UIButton!
// MARK: - IBAction
@IBAction func showOnMapPressed(_ sender: UIButton) {
toggleMap()
}
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
}
}
extension TripCell {
fileprivate func toggleMap() {
if state == .defaultState {
mapHeightConstraint.priority = UILayoutPriority(999)
state = .mapState
}
else {
mapHeightConstraint.priority = UILayoutPriority(250)
state = .defaultState
}
delegate.updateTable(index)
}
}
这是DashboardViewController扩展
extension DashboardViewController: UITableViewDelegate, UITableViewDataSource {
internal func setupTableView() {
tableView.register(UINib(nibName: "TripCell", bundle: nil), forCellReuseIdentifier: "TripCellID")
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = UITableView.automaticDimension
// tableView.allowsMultipleSelection = true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TripCellID", for: indexPath) as! TripCell
TripFunctions.readTrips {
cell.origin.text = Data.trips[indexPath.row].title
}
// for reloading row
cell.delegate = self
cell.index = indexPath
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// if let selectedIndexPaths = tableView.indexPathsForSelectedRows, selectedIndexPaths.contains(indexPath) {
// return 220
// }
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
updateTable(indexPath)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
updateTable(indexPath)
}
func updateTable(_ index: IndexPath) {
tableView.beginUpdates()
tableView.reloadRows(at: [index], with: .automatic)
tableView.endUpdates()
}
}