Having a UITextField in a UITableViewCell
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Putting a UITextField inside a UITableViewCell is a common pattern for forms, settings screens, and editable lists. The main challenge is not placing the text field on screen. It is keeping the text in sync with your data model while cells are reused as the table scrolls.
Use A Custom Cell
Start with a custom cell subclass that owns the text field. Keep the layout and the text-input wiring inside the cell rather than scattering it through the view controller.
The important part is the callback. The cell should not be the source of truth for the data.
Keep State In The Model, Not The Cell
Because table view cells are reused, storing the text only inside the visible UITextField is a bug waiting to happen. Instead, keep the current value in your backing model.
Now when a cell scrolls off screen and comes back, its text is restored from the model.
Be Careful With Reuse
The most common bugs come from reuse:
- text appearing in the wrong row
- callbacks writing to the wrong index path
- first responder jumping during reloads
If rows can be inserted, deleted, or reordered while editing, storing the row number inside the closure can become fragile. In more dynamic tables, update the model using a stable item identifier rather than the current index path.
Keyboard And Focus Management
When a text field becomes first responder, the keyboard may cover part of the table. A few standard improvements help:
- use
keyboardDismissMode = .onDragfor smoother dismissal - scroll the active cell into view if needed
- avoid full table reloads while the user is editing
For example:
If you call reloadData() after every keystroke, the current cell may be recreated and editing will feel broken. Update only the model unless the UI truly needs a partial row refresh.
Delegate Versus Closure
Closures are convenient for small examples, but a delegate protocol is also fine if you want clearer ownership or if the cell needs to report more than one event.
The design rule stays the same:
- the cell reports user input
- the controller or view model owns the form state
That separation keeps reuse manageable.
Common Pitfalls
- Treating the text field inside the cell as the only copy of the value.
- Calling
reloadData()during editing and disrupting first responder state. - Writing changes back to the wrong row after inserts or deletes.
- Forgetting that reused cells must be fully reconfigured in
cellForRowAt. - Mixing layout code, input handling, and persistence logic in one oversized view controller.
Summary
- A
UITextFieldinside aUITableViewCellworks well when the cell is custom and reusable. - Keep the real value in your model, not only in the visible text field.
- Reconfigure every reused cell from model state in
cellForRowAt. - Avoid unnecessary reloads while the user is typing.
- Use closures or delegates to send text changes upward, but keep data ownership outside the cell.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.