gpt4 book ai didi

ios - 如何在 UITableView 的底部只设置一个 Table Cell?

转载 作者:可可西里 更新时间:2023-11-01 02:12:17 24 4
gpt4 key购买 nike

我只想在我的 UITableView 底部设置一个 UITableCell。像这样:

+----------------+
| +------------+ |
| | cell 1 | |
| +------------+ |
| +------------+ |
| | cell 2 | |
| +------------+ |
| +------------+ |
| | cell 3 | |
| +------------+ |
| |
| |
| |
| |
| |
| |
| +------------+ |
| | Last cell | |
| +------------+ |
+----------------+

但是我不知道如何实现这个。

我看过:

但是所有这些都是为了设置 UITableView 底部的所有单元格,而我无法对只有一个 单元格进行设置。

是否可以将最后一个单元格设置在 UITableView 的底部?我怎样才能实现它?

编辑: 我已经按照评论的建议尝试了两个部分,但它重复了 cell 1cell 2 的空间第二部分的单元格 3

我希望第二部分只有最后一个单元格,而不是等于其余单元格高度的空间。之后,我想在第一节的 footerView 中添加一个特定的空间。

提前致谢!

最佳答案

我想你需要的是这样的:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!
var array = ["1","2","3","4"]
var dummyInserted = false

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.reloadData()
if array.count < 15 {
array.insert("", at: array.count-1)
dummyInserted = true
tableView.reloadData()
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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

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

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

let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier")

cell?.textLabel?.text = array[indexPath.row]

return cell!
}

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

if dummyInserted == true {

if indexPath.row == array.count-2 {

return tableView.bounds.size.height - CGFloat(((array.count-1) * 44))
}
}

return 44
}
}

因此,如果您知道您的 TableView 可以容纳 15 个可见行(也许您必须计算),那么您可以

1) 如果最后一个单元格可见,则在数组倒数第二个索引处插入一个虚拟值

2) 将该虚拟行的高度设置为最后一个值位于 TableView 底部的高度

也许你必须更改一些代码,但它应该为你的想法提供一个良好的起点

应该是这样的

enter image description here

关于ios - 如何在 UITableView 的底部只设置一个 Table Cell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653534/

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