【发布时间】:2018-01-04 17:41:37
【问题描述】:
使用 Swift4、iOS11.2.1、Xcode9.2,
我成功地将自定义按钮添加到 tableView 的最后一个单元格。 (该按钮用于在 tableView 中添加单元格 - 这也可以正常工作......) - 参见 Screenshot-1。
但现在的问题是:当添加更多单元格时(即超过 tableView-height 的容量),并且如果您滚动到顶部,则会有第二个单元格显示此按钮 - 请参阅 Screenshot-2。
(见下面的代码......)
这是两张截图:
我怎样才能摆脱多余的按钮???? (似乎重复使用了一个单元格并且部分绘制了按钮。截止可以解释,因为最后一个单元格的高度大于其他单元格。但是,任何按钮中都不应该有部分按钮除了最后一个单元格之外的其他单元格......即使向上滚动)。
任何帮助表示赞赏!
这是我的代码(或它的摘录...):
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!
}
【问题讨论】:
-
我只需在footerView 中添加按钮,这样它就始终位于底部且更易于实现
-
是的,单元格被重复使用,并且您在单元格中一遍又一遍地添加按钮,如果不需要它们,请永远不要删除它们。我会在 Interface Builder 中使用按钮设计第二个单元格,并将其用于最后一个索引路径。并强制解开单元格
as! SettingsCustomTableViewCell。无论如何,在方法结束时你都会这样做,你会摆脱所有丑陋的问号。
标签: swift tableview cell indexpath