我在这里读过一些关于为什么会发生这种情况的解释,但我无法找到解决方案,这可能非常简单。
我有一个
UITableViewController
它有两个部分,每个部分有20行。
我想添加一个
UISwitcher
在第二行中,我实现了以下内容:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? SettingsCell else {
return UITableViewCell()
}
switch indexPath.row {
case 2: cell.addSwitcher()
default: break
}
return cell
}
在我上下滚动之前,一切都按预期工作,过一段时间后,我会在每一行中都有切换器。
在
dequeueReusableCell
还是我在这里丢了什么东西?
这是我的
addSwitcher()
功能
public func addSwitcher() {
accessoryView = switcher
switcher.addTarget(self, action: #selector(switcherDidChange(switcher:)), for: .valueChanged)
hasSwitcher = true
}