我观察到,当在表视图行中使用UISwitch时,切换动画并不总是完成。开关的背景保持灰色而不是再次变白。 video和下图显示了问题。有谁知道为什么会这样?这似乎是一个错误,但也许我错过了一些东西。
代码可用here。
我认为这是主线程上的可重用问题。您可以使用下面的代码,其中switch操作在主线程异步上运行
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell {
cell.theSwitch.row = indexPath.row
DispatchQueue.main.async {
cell.theSwitch.setOn(self.array[indexPath.row], animated: false)
}
return cell
}
return UITableViewCell()
}