[英]Swift - Open ViewController on Tapping a TableViewCell
[英]HeaderView tapping tableviewcell expand
我試圖通過點擊 header 單元格來擴展 tableviewcell。 但我想知道如何通過 header 委托 function 中的部分來傳遞折疊布爾值。 當我點擊 header 時,代表 function 正在正確調用。
請糾正。
class 視圖控制器:UIViewController,UIGestureRecognizerDelegate {
//MARK: - IBOutlets
@IBOutlet weak var bottomView: UIView!
@IBOutlet weak var subcriptionTableview: UITableView!
var reloadSections: ((_ section: Int) -> Void)?
var isCollapsible = false
var isCollapsed = false
let imgArray = ["basic","standard","advanced","premium"]
let amtArray = ["$10 / month","$25 / month","$30 / month","$40 / month"]
let planArray = ["Basic","Standard","Advanced","Premium"]
let expandViewArray = ["Lorem ipsum is a dummy text content in a paragraph.","Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium.","At vero eos et accusamus et iusto odio dignissimos."]
//MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
reloadSections = { [weak self] (section: Int) in
self?.subcriptionTableview?.beginUpdates()
self?.subcriptionTableview?.reloadSections([section], with: .automatic)
self?.subcriptionTableview?.endUpdates()
}
subcriptionTableview.layer.cornerRadius = 10
subcriptionTableview.register(UINib(nibName: "SubsPlanHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: "SubsPlanHeader")
subcriptionTableview.register(UINib(nibName: "ExpandTableViewCell", bundle: nil), forCellReuseIdentifier: "expandcell")
bottomView.clipsToBounds = true
bottomView.layer.cornerRadius = 15
bottomView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
}
}
擴展 ViewController: UITableViewDelegate,UITableViewDataSource,HeaderViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return planArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard isCollapsible else{
return expandViewArray.count
}
if isCollapsed == true {
return 0
}else{
return expandViewArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = subcriptionTableview.dequeueReusableCell(withIdentifier: "expandcell", for: indexPath) as! ExpandTableViewCell
cell.labelPlanDetail.text = expandViewArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = subcriptionTableview.dequeueReusableHeaderFooterView(withIdentifier: "SubsPlanHeader") as! SubsPlanHeader
headerCell.labelAmt.text = amtArray[section]
headerCell.imgView.image = UIImage(named: imgArray[section])
headerCell.labelPlanName.text = planArray[section]
headerCell.labelLine.isHidden = true
headerCell.delegate = self
headerCell.section = section
return headerCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 150
}
func toggleSection(header: SubsPlanHeader, section: Int) {
if isCollapsible {
// Toggle collapse
let collapsed = !isCollapsed
isCollapsed = collapsed
// header.setCollapsed(collapsed: collapsed)
// Adjust the number of the rows inside the section
reloadSections?(section)
}
}
}
UITableViewHeaderFooterView:
協議 HeaderViewDelegate: AnyObject { func toggleSection(header: SubsPlanHeader, section: Int) }
class SubsPlanHeader: UITableViewHeaderFooterView {
// MARK: - IBOutlets
@IBOutlet weak var subView: UIView!
@IBOutlet weak var labelAmt: UILabel!
@IBOutlet weak var imageArrow: UIImageView!
@IBOutlet weak var imageCheck: UIImageView!
@IBOutlet weak var labelPlanName: UILabel!
@IBOutlet weak var labelLine: UILabel!
@IBOutlet weak var imgView: UIImageView!
var section: Int?
weak var delegate: HeaderViewDelegate?
override func awakeFromNib() {
super.awakeFromNib()
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapHeader)))
}
@objc private func didTapHeader() {
if let _section = section{
delegate?.toggleSection(header: self, section: _section)
print("touched")
}
}
}
提前致謝。
在我看來,您無法按照您的意願執行委托方法,因為自 viewController 啟動以來,您的屬性isCollapsible
始終為false
。 只有當它是true
時你才會切換它
任何方式,而不是
let collapsed = !isCollapsed
isCollapsed = collapsed
您可以使用
isCollapsed = !isCollapsed
因為通過點擊 header 您總是需要重新加載部分以進行隱藏/取消隱藏嘗試刪除if condition
並重新考慮一些代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.