我有一个包含多个部分的 tableView,每个部分仅包含一个单元格(这样做是为了在每个单元格之间创建一个间隙)。我试图在选择单元格时为它创建一个自定义选择 View 。 当我选择一行时,自定义选择 View 将被添加到多个单元格中。我知道问题是因为细胞被重复使用。解决这个问题的最佳解决方案是什么?
这是我的代码。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let myCell = tableView.cellForRowAtIndexPath(indexPath) as? PredefinedServicesCell{
let selectionView = UIView()
selectionView.backgroundColor = UIColor(hex: 0x3399CC).colorWithAlphaComponent(0.2)
selectionView.layer.cornerRadius = (myCell.containerView.layer.cornerRadius)
selectionView.frame = (myCell.containerView.frame)
myCell.containerView.addSubview(selectionView)
}
}
最佳答案
您可以在您的 CustomCell 类中使用以下方法隐藏或显示自定义选定 View
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.customSelectedView.hidden = !selected
}
关于ios - didSelectRowAtIndexPath 带有包含部分的 tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38160016/