如何禁用UITableViewCell自定义动画?调用UIView.setAnimationsEnabled(false)无效
Got it, let's work through this problem. The reason UIView.setAnimationsEnabled(false) in viewDidLoad didn't work is twofold: first, the system often resets this animation flag automatically at various points (like after view transitions), and second, your custom animation in willDisplay is likely wrapped in your own UIView.animate block, which doesn't always respect that global toggle reliably.
Here are three targeted solutions to disable the cell animation exactly when you need it:
1. Add a Boolean Flag to Control Animation Execution
This is the most straightforward and flexible approach—you explicitly decide when to run the animation by checking a flag right before executing your animation code.
class YourTableVC: UITableViewController { // Create a flag to toggle animation state private var shouldSkipCellAnimations = false override func viewDidLoad() { super.viewDidLoad() // Disable animations for the initial load shouldSkipCellAnimations = true tableView.reloadData() // Re-enable animations after the initial load completes DispatchQueue.main.async { self.shouldSkipCellAnimations = false } } override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { // Only run the animation if the flag allows it guard !shouldSkipCellAnimations else { return } // Your existing custom animation code cell.transform = CGAffineTransform(translationX: 50, y: 0) cell.alpha = 0.0 UIView.animate(withDuration: 0.3) { cell.transform = .identity cell.alpha = 1.0 } } }
Just set shouldSkipCellAnimations = true whenever you need to disable the animation (like before a specific reload, or when a certain condition is met), then flip it back to false when you want animations to resume.
2. Wrap Table View Updates in performWithoutAnimation
If you only need to disable animations for a specific table view update (like a one-time reload or cell insertion), wrap that update in UIView.performWithoutAnimation. This temporarily disables all animations within the block, including your custom willDisplay animation.
// Call this when you need to update the table without animations UIView.performWithoutAnimation { tableView.reloadData() // Or any other table operation: insertRows, deleteRows, etc. }
This works because UIView.performWithoutAnimation suppresses all animation blocks executed during its scope—your custom UIView.animate code in willDisplay will jump straight to the final state instead of animating.
3. Check for Specific Index Paths or Conditions
If you need to disable animations only for certain cells (not all), add a check for index paths or other conditions in willDisplay:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { // Disable animation for the first 3 rows, for example if indexPath.row < 3 { cell.alpha = 1.0 // Set to final state immediately cell.transform = .identity return } // Run animation for other cells cell.transform = CGAffineTransform(translationX: 50, y: 0) cell.alpha = 0.0 UIView.animate(withDuration: 0.3) { cell.transform = .identity cell.alpha = 1.0 } }
Pick the method that fits your specific use case—flag-based control is great for global toggles, performWithoutAnimation works for one-time updates, and condition checks are perfect for cell-specific rules.
内容的提问来源于stack exchange,提问作者Adrian

