ios - 如果超过其 indexpath.row,如何在 TableView 中设置单元格行高?

标签 ios swift uitableview

我正在尝试使用 TableView 制作常见问题解答 View Controller ,我需要在 UI 中进行一些修复。这是我的 FAQ VC 现在的展示

(请忽略红线)

enter image description here

enter image description here

如我们所见,基本上有 2 种行高,80 和 160。如果点击(选择)行,行高将从 80(黄线)扩展到 160(紫线)。

我想把最后一题下面的行高设置成80,不是160,我试过了,但是最后一题下面的行不能设置。这是我使用的代码

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
        }
    }

}

最佳答案

请将此代码放入 Controller 的 viewDidLoad() 方法中。

YOUR_TABLE_VIEW.tableFooterView = UIView(frame: .zero)

这将删除额外的分隔符。

关于ios - 如果超过其 indexpath.row,如何在 TableView 中设置单元格行高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49271646/

相关文章:

swift - UIRefreshControl 刷新坚持旋转

ios - 是否可以为不同的尺寸类别设置不同的表格 View 单元格行高?

ios - NSInvocationOperation 回调太快

ios - 默认调用 UITableViewDataSource/Delegate 方法

Swift,在展开可选值时意外发现 nil (NSKeyedUnarchiver)

ios - 表达式类型不明确,没有更多上下文(枚举)

ios - 从另一个 "getDocument"查询中的 "getDocument"查询中检索数据

ios - 防止 UITableView 行被销毁

iphone - 当我点击连续的开关时,如何获取indexPath.row/path?

objective-c - AQGridView iOS 创建自定义图标(图像+标签)而不是图像