How to get the indexpath.row when an element is activated?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a UITableView, the way you get indexPath.row depends on what was activated. If the user tapped the row itself, UIKit already gives you the index path. If the user tapped a control inside the cell, you need either a callback from the cell or a way to map that control back to its row.
Use the Delegate When the Row Was Tapped
If the row itself is selected, the simplest answer is the built-in delegate method.
If your interaction is row selection, do not invent a workaround. This is already the correct API.
Convert a Control Position Back to the Table View
If the tap comes from a button, switch, or other control inside the cell, you do not automatically receive an index path. One common technique is to convert the sender position into the table view’s coordinate system.
This works because indexPathForRow(at:) can tell you which row occupies that point. In cellForRowAt, wire the button target like this:
This approach is practical and works well when the controller owns the action.
Prefer a Cell Callback for Cleaner Design
For more maintainable code, the cell can expose a closure or delegate back to the controller. That avoids view-hierarchy tricks and keeps the flow explicit.
In the controller:
In many apps, the best version is to pass the model or item id instead of the row number. Rows can move; model identity is usually more stable.
Avoid Tags for Mutable Tables
A common shortcut is button.tag = indexPath.row, then reading sender.tag later. That can work in a tiny static table, but it becomes fragile with:
- multiple sections
- insertions and deletions
- cell reuse
- diffable data sources
Once rows move, the stored tag may no longer match the item the user sees on screen.
Common Pitfalls
- Recomputing the row manually when
didSelectRowAtalready gives you the fullIndexPath. - Using tags as if row numbers were permanent identifiers.
- Forgetting that multi-section tables need
sectionas well asrow. - Walking the superview chain instead of using a cleaner callback or coordinate conversion.
- Passing row numbers around when the underlying model object would be a safer reference.
Summary
- Use
didSelectRowAtwhen the row itself is selected. - For controls inside a cell, convert the control position to a table-view point or use a cell callback.
- Keep the full
IndexPathwhen sections matter. - Avoid tag-based solutions in tables that can change over time.
- When possible, pass the model or item id instead of the row number.
Related reading
- How to get the indexpath.row when an element is activated?
- How to get the iPhone's screen width in SwiftUI?
- How to get the name of enumeration value in Swift?
- How to get the name of the running application in iOS
- How to get the Power of some Integer in Swift language?
- How to get the result of OnPostExecute to main activity because AsyncTask is a separate class?
- How to get the screen width and height in iOS?
- How to get the scroll bar with CSS overflow on iOS
.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.