【发布时间】:2016-07-23 18:12:41
【问题描述】:
我在使用 tableview 上的按钮时遇到了一点问题。
我有一个用 3 个按钮自定义的 tableViewCell。我在界面生成器中将按钮设置为隐藏,当表格加载时,按钮按预期隐藏。
然后,我在调用 didSelectRow 时将 tableview 的 hidden 属性设置为 false,在调用 didDeselectRow 时将其设置为 hidden.true。这也很好。问题是在 didSelectRow 中设置为可见的按钮也每隔七个单元格可见。他们不断重复自己。
下面是显示按钮的代码
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ContactsViewCell
print("Table selected")
cell.insertEmailButton.hidden = false
cell.insertPhoneButton.hidden = false
cell.insertAllButton.hidden = false
cell.contactTextLabel.alpha = 0.2
cell.contactDetailTextLabel.alpha = 0.2
}
当 tableViewCell 被取消选择时,这会隐藏它们
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ContactsViewCell
cell.insertEmailButton.hidden = true
cell.insertPhoneButton.hidden = true
cell.insertAllButton.hidden = true
cell.contactTextLabel.alpha = 1.0
cell.contactDetailTextLabel.alpha = 1.0
}
我做了一些研究,发现可能是buttons.hidden 设置为false 的行被tableview 重用了。但我从文档中了解到,被重用的单元格来自 cellForRowAtIndexPath 而不是 didSelectRow 处的单元格,我将 button.hidden 设置为 false。
我还尝试在 cellForRowAtIndexPath 的 if else 语句中使用 cell.isSelected 属性来隐藏和显示按钮,但这根本不显示按钮。
提前感谢您的帮助
【问题讨论】:
标签: ios swift uitableview uibutton