gpt4 book ai didi

ios - TableView 显示问题 - Swift

转载 作者:行者123 更新时间:2023-11-30 11:59:20 24 4
gpt4 key购买 nike

我在 Swift 中有一个 TableView ,其标题可以扩展或收缩以显示或隐藏单元格,但由于某种原因,页面中间无缘无故有额外的行分隔符,并且标题中的标签没有显示向上。

enter image description here

class ExpandableHeader: UITableViewHeaderFooterView {
var section: Int = 0
let expandLabel = UILabel()
}

func numberOfSections(in tableView: UITableView) -> Int {
// return number of section in table from data
return list.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return number of rows in each section from data
return list[section].items.count + 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// Create cell with the identifier which was set in the storyboard prototype cell

// set cell data/name from our data object
if indexPath.row < list[indexPath.section].items.count {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = list[indexPath.section].items[indexPath.row].name
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "custom", for: indexPath)
return cell
}

}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

// create a header of type of our subclassed header with section number
let headerView = ExpandableHeader()

headerView.expandLabel.text = "+"

headerView.expandLabel.frame.size.height = 30
headerView.expandLabel.frame.size.width = 30
headerView.expandLabel.textAlignment = NSTextAlignment.center

headerView.addSubview(headerView.expandLabel)

headerView.expandLabel.frame.origin.x = view.frame.maxX - headerView.expandLabel.frame.width

// assign selected/current section number to the header object
headerView.section = section

// create Gesture Recognizer to add ability to select header to this cutsom header with an action
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(headerClicked(sender:)))

// add Gesture Recognizer to our header
headerView.addGestureRecognizer(tapGesture)

return headerView
}

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

// check if row's section expanded parameter is set to true or false. set height of rows accordingly to hide or show them
if list[indexPath.section].expanded == true {
return 44
} else {
return 0
}
}

我在上面添加了相关的代码行。当您将行高更改为 0 时,单元格分隔线是否也应该更改为高度 0,或者隐藏?

当 Storyboard 一直到右边缘时,为什么每个部分的标题没有一直到屏幕的右侧?这也是标题文本标签未显示的原因吗?因为右边缘被切断了?

抱歉,我提出了一些基本问题,我仍在掌握其中的窍门,所以任何建议将不胜感激。

最佳答案

要隐藏额外的行分隔符,请使用下面的代码行

 let backgroundView = UIView(frame: CGRect.zero)
self.tableView.tableFooterView = backgroundView //instead of tableView give your tableView name

关于ios - TableView 显示问题 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47452678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com