我正在尝试使用 TableView 制作常见问题解答 View Controller ,我需要在 UI 中进行一些修复。这是我的 FAQ VC 现在的展示
(请忽略红线)
如我们所见,基本上有 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/