gpt4 book ai didi

swift - 在滚动时显示两次的最后一个 tableView 单元格中添加按钮

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:29 24 4
gpt4 key购买 nike

使用Swift4, iOS11.2.1, Xcode9.2,

我成功地将自定义按钮添加到 tableView 的最后一个单元格。 (该按钮用于在 tableView 中添加单元格 - 这也可以正常工作...) - 请参见屏幕截图 1。

但现在的问题是:当添加更多单元格(即超过 tableView 高度)时,如果您滚动到顶部,则会有第二个单元格显示此按钮 - 请参见屏幕截图 2。

(见下面的代码....)

下面是两张截图:

enter image description here

我怎样才能摆脱多余的按钮????(似乎一个单元格被重复使用并且按钮被部分绘制。可以解释截止,因为最后一个单元格的高度比其他单元格大。但是,任何按钮都不应该有部分除了最后一个单元格之外的其他单元格......即使向上滚动)。

感谢任何帮助!

这是我的代码(或它的摘录......):

override func viewDidLoad() {
super.viewDidLoad()

// define delegates
self.fromToTableView.dataSource = self
self.fromToTableView.delegate = self

// define cell look and feel
self.addButton.setImage(UIImage(named: "addButton"), for: .normal)
self.addButton.addTarget(self, action: #selector(plusButtonAction), for: .touchUpInside)

//...

self.fromToTableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

guard let resItems = resItems else {
self.rowCount = 0
return 0
}
self.rowCount = resItems.count - 1
return resItems.count
}

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

if(indexPath.row == (self.rowCount)) {
return 160
} else {
return 120
}
}

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

let cell = fromToTableView.dequeueReusableCell(withIdentifier: "SettingsCell") as? SettingsCustomTableViewCell

// configure the cell
cell?.configureCell(tag: indexPath.row)

// fill cell-data
if let resItem = self.resItems?[indexPath.row] {
cell?.connectionItem = resItem
}

// add addButton to last cell
if(indexPath.row == (self.rowCount)) {

// add addButton
cell?.contentView.addSubview(self.addButton)
// scroll to last cell (that was just added)
// First figure out how many sections there are
let lastSectionIndex = self.fromToTableView.numberOfSections - 1
// Then grab the number of rows in the last section
let lastRowIndex = self.fromToTableView.numberOfRows(inSection: lastSectionIndex) - 1
// Now just construct the index path
let pathToLastRow = IndexPath(row: lastRowIndex, section: lastSectionIndex)
// Scroll to last cell
self.fromToTableView.scrollToRow(at: pathToLastRow as IndexPath, at: UITableViewScrollPosition.none, animated: true)
}

return cell!
}

最佳答案

你已经知道问题出在哪里了:)

正如你在这里所说:

it seems that a cell is reused and the button is partly drawn

这正是这里发生的事情。您有一个 UITableViewCell 元素池,或者在您的情况下为 SettingsCustomTableViewCell。所以每当你说:

let cell = fromToTableView.dequeueReusableCell(withIdentifier: "SettingsCell") as? SettingsCustomTableViewCell

然后,顾名思义,您从 UITableView 中取出一个可重复使用的单元格。

这也意味着您之前在单元格上设置的任何内容(例如您的按钮)都会在您重复使用那个单元格的特定实例时保留在那里。

您可以(至少)通过三种方式解决此问题。

黑客

在你的 tableView(_:cellForRowAt:indexPath:) 你有这个:

if(indexPath.row == (self.rowCount)) {
//last row, add plus button
}

您可以添加 else 部分并再次删除加号按钮:

if(indexPath.row == (self.rowCount)) {
//last row, add plus button
} else {
//not last row, remove button
}

正确的方法

UITableViewCell 具有函数 prepareForReuse正如文档所说:

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCell(withIdentifier:)

因此,这是您“重置”自定义表格 View 单元格的地方。在您的情况下,您删除了加号按钮。

您似乎已经有了一个自定义的 UITableViewCell,所以这应该是一个小改动。

页脚方式

正如@paragon 在他的评论中建议的那样,您可以创建一个 UIView 并将其作为 tableFooterView 添加到您的 UITableView

let footerView = YourFooterViewHere()
tableView.tableFooterView = footerView

希望对你有帮助

关于swift - 在滚动时显示两次的最后一个 tableView 单元格中添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100740/

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