swift 可展开的表视图单元格(FAQ样式)

u59ebvdq  于 2022-12-03  发布在  Swift
关注(0)|答案(2)|浏览(159)

我有一个FAQ样式的表格视图,其中每个单元格包含两个UILabel-一个用于问题,一个用于答案。现在,问题和答案显示在viewDidLoad()上。使用didSelectRowAt()方法,我想我可以在单击单元格时隐藏答案UILabel,但它似乎不起作用。点击单元格时没有任何变化。

编码:

class FAQsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var faqsTableView: UITableView!
    
    struct faqStructure {
        let question: String
        let answer: String
    }
    
    var faqsArray: [faqStructure] = []
    
    override func viewDidLoad() {
        
        faqsTableView.rowHeight = UITableView.automaticDimension
        faqsTableView.estimatedRowHeight = 600
        faqQuery()
    }
    
    @IBAction func dismiss(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    func faqQuery() {
        let faqsDB = Database.database().reference().child("FAQs")
        faqsDB.observe(.value, with: { (snapshot) in
            self.faqsArray.removeAll()
            for child in snapshot.children {
                let faqSnapshot = child as! DataSnapshot
                let faqQuestion = faqSnapshot.childSnapshot(forPath: "Question").value as! String
                let faqAnswer = faqSnapshot.childSnapshot(forPath: "Answer").value as! String
                self.faqsArray.append(.init(question: faqQuestion, answer: faqAnswer))
                self.faqsTableView.reloadData()
            }
        })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return faqsArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let faqItem = faqsTableView.dequeueReusableCell(withIdentifier: "FAQ Cell") as! FAQsItemCell
        faqItem.questionLabel.text = faqsArray[indexPath.row].question
        faqItem.answerLabel.text = faqsArray[indexPath.row].answer
        
        return faqItem
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let faqItem = faqsTableView.dequeueReusableCell(withIdentifier: "FAQ Cell") as! FAQsItemCell
        faqItem.answerLabel.isHidden = true
    }
}
jm2pwxwz

jm2pwxwz1#

发现了一个非常好的文章,让它完美地工作与最小的变化,我的代码!
https://medium.com/@alexiscreuzot/expanding-uitableview-cells-using-only-constraints-in-swift-f40b13206ea3
在TableViewItemCell的程式码中,关键是要有一个布林值,指出储存格是否已展开:

class FAQExpandableItem {
    var question: String
    var answer: String
    var isExpanded: Bool
    
    init(question: String, answer: String) {
        self.question = question
        self.answer = answer
        self.isExpanded = false
    }
}

class FAQsItemCell: UITableViewCell {

    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var answerLabel: 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
    }
    
    func set(content: FAQExpandableItem) {
        self.questionLabel.text = content.question
        self.answerLabel.text = content.isExpanded ? content.answer : ""
    }
}

然后在ViewController中,我们使用didSelectRowAt方法来切换此布尔值:

class FAQsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var faqsTableView: UITableView!
    
    struct faqStructure {
        let question: String
        let answer: String
    }
    
    var faqsArray: [faqStructure] = []
    var faqData = [FAQExpandableItem]()
    
    override func viewDidLoad() {
        faqsTableView.rowHeight = UITableView.automaticDimension
        faqsTableView.estimatedRowHeight = 600
        faqQuery()
    }
    
    @IBAction func dismiss(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    func faqQuery() {
        let faqsDB = Database.database().reference().child("FAQs")
        faqsDB.observe(.value, with: { (snapshot) in
            self.faqsArray.removeAll()
            for child in snapshot.children {
                let faqSnapshot = child as! DataSnapshot
                let faqQuestion = faqSnapshot.childSnapshot(forPath: "Question").value as! String
                let faqAnswer = faqSnapshot.childSnapshot(forPath: "Answer").value as! String
                self.faqData.append(FAQExpandableItem(question: faqQuestion, answer: faqAnswer))
                self.faqsTableView.reloadData()
            }
        })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return faqData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let faqItem = tableView.dequeueReusableCell(withIdentifier: "FAQ Cell", for: indexPath) as! FAQsItemCell
        faqItem.set(content: faqData[indexPath.row])
        return faqItem
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let content = faqData[indexPath.row]
        content.isExpanded = !content.isExpanded
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
}
umuewwlo

umuewwlo2#

您可以使用节和行的组合来展开和折叠行为。这是相当有效的。您可以在互联网上搜索可展开的表格视图了解更多详细信息
总之,问题出在你的didselect方法上
更改此行

let faqItem = faqsTableView.dequeueReusableCell(withIdentifier: "FAQ Cell") as! FAQsItemCell

结束日期

guard let faqItem = faqsTableView.cellForRow(at: indexPath) as? FAQsItemCell else { return }

///使用保护以提高安全性
我相信隐藏标签不会降低单元格高度,而且您没有存储结果(即隐藏/显示状态),因此它将导致dequeueReusableCell出现问题。

相关问题