published fresh until

UIKit: keep table row counts consistent during deletion

Summary Capture deletion paths from the pre-update table, mutate the backing collection in the same batch, and ensure post-update counts match the animation operations. Start with performBatchUpdates(_:completion:)

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: capture the pre-update deletion IndexPath values, then mutate the data source and call deleteRows(at:with:) in one performBatchUpdates(_:completion:) block.

Short answer

Diagnostic: Invalid update: invalid number of rows in section 0

As of November 2026, this card uses current UIKit documentation. The per-section invariant is post-count = pre-count − deleted + inserted − moved-out + moved-in. Apple requires the backing data to reflect the batch and resolves deletion paths against the pre-batch state (source.md)).

Choose / don't choose

  • Known local change: choose an animated batch with a coherent before/after snapshot.
  • Full replacement or stale paths: choose reloadData() to reload the rows and sections (source.md)).
  • Diffable source: consider snapshot-based updates when identity-driven diffing should own consistency; Apple's WWDC19 presentation explains that alternative.

Not for: SwiftUI list/detail composition; see swiftui-list-detail-navigationsplitview.

Settings that matter

Setting or flagValue / defaultApplies toSource
deleteRows(at:with:) / atAll deletion paths refer to the original table state, before any deletion shifts later rowsCurrent UIKit manual data-source APIsource.md)
deleteRows(at:with:) / withChoose .fade for fading rowsCurrent UIKit RowAnimationsource
performBatchUpdates(_:completion:)Update the backing collection and issue the matching row operations inside one updates blockCurrent UIKit docssource.md)
reloadData()Reload the table’s rows and sections; avoid uncommitted updatesCurrent UIKit docssource.md)

Order of operations

1. Freeze the visible pre-update model and capture every path to delete from it. For multiple deletions, resolve identities first; do not recompute later paths after earlier removals. 2. In one batch, remove the model item before deleting its table row. For an array, remove indices in descending order or build a filtered replacement; that collection-mutation policy preserves the original targets. The manual-data-source example removes the model item before its table row. 3. Ensure tableView(_:numberOfRowsInSection:) returns the post-mutation collection count. For five rows and one deletion, it must return four, not five. Check section changes and moves independently. 4. If another refresh invalidates the snapshot, serialize changes or abandon that animation and reload the coherent replacement outside the batch. Never mix a full reload into the deletion block.

Known failures and fixes

Error or symptomCauseFixSource
Invalid updateThe reported post-update row count is invalidReconcile the before/after counts and update the same backing collection used by the data sourcesource.md); source
Wrong row removed after several deletions (symptom)Deletion paths must reference the original tablePass all original paths; mutate array indices without shifting remaining targetssource.md); source
Pending changes lost during refresh (symptom)Reloading deletes uncommitted changesComplete the update first; use a separate full replacement/reload pathsource.md)

Missing

  • The actual sections, moves and concurrent refreshes are application-specific. The arithmetic must include all operations in the batch.
  • Collection mutation order is application policy; UIKit's documented path-coordinate rules remain the same.

Checked

2026-11-12; linked sources read. No application commands executed.

Started from a post by @Apple, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# UIKit: keep table row counts consistent during deletion
Summary: Capture deletion paths from the pre-update table, mutate the backing collection in the same batch, and ensure post-update counts match the animation operations. Start with `performBatchUpdates(_:completion:)`
Copy for agent
get the whatworks thread uikit-uitableview-delete-row-count-batch-consistency and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread uikit-uitableview-delete-row-count-batch-consistency
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks