如果超出其indexpath.row,如何在表格视图中设置单元格行高? [英] how to set cell row height in table view if it exceed its indexpath.row?

查看:27
本文介绍了如果超出其indexpath.row,如何在表格视图中设置单元格行高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用表格视图制作 FAQ 视图控制器,我需要在 UI 中进行一些修复.这是我的 FAQ VC 现在的显示

I am trying to make a FAQ view controller using table view, I need little bit fix in the UI. here is the display of my FAQ VC right now

(请忽略红线)

正如我们所见,基本上有 2 行高,80 &160.如果行被点击(选择),行高将从80(黄线)扩大到160(紫线).

as we can see, basically there are 2 row height, 80 & 160. if the row is tapped (selected) the row height will expand from 80 (yellow line) to 160 (purple line).

我想让最后一个问题下的行高仍然是80而不是160.我试过但我无法设置最后一个问题下的行.这是我使用的代码

I want to make the row height under the last question is still 80 not 160. I have tried but I can't set the row below the last question. here is my code I use

class FAQVC: UIViewController {

    @IBOutlet weak var retryButton: DesignableButton!
    @IBOutlet weak var tableView: UITableView!

    var FAQs = [FAQ]()
    var selectedIndexs: [IndexPath: Bool] = [:]



    override func viewDidLoad() {
        super.viewDidLoad()

        getFAQData()


    }

}

extension FAQVC : UITableViewDelegate, UITableViewDataSource {

    // MARK: - Table View Delegate and Datasource


    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return FAQs.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FAQCell") as! FAQCell
        cell.FAQData = FAQs[indexPath.row]

        return cell
    }



    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if self.cellIsSelected(indexPath: indexPath) {
          return 160
        } else {
            return 80
        }


    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let isSelected = !self.cellIsSelected(indexPath: indexPath)
        selectedIndexs[indexPath] = isSelected



        tableView.beginUpdates()
        tableView.endUpdates()
    }


    func cellIsSelected(indexPath: IndexPath) -> Bool {
        if let number = selectedIndexs[indexPath] {
            return number
        } else {
            return false
        }
    }

}

推荐答案

请将此代码放入控制器的 viewDidLoad() 方法中.

Please put this code in your viewDidLoad() method of your controller.

YOUR_TABLE_VIEW.tableFooterView = UIView(frame: .zero)

这将删除额外的分隔符.

this will remove extra separators.

这篇关于如果超出其indexpath.row,如何在表格视图中设置单元格行高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆