Get button click inside UITableViewCell
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Handling a button tap inside a UITableViewCell is a small problem that exposes a bigger UIKit rule: cells are reusable views, not the source of truth for application state. The clean solution is to let the cell report the tap and let the table view controller decide what that tap means for the underlying data.
A Reliable Pattern
The simplest modern pattern is to give the cell a callback or delegate. The cell handles the button press locally, and the controller supplies the action when configuring the cell.
This keeps the cell reusable and focused. It knows that a button was tapped, but it does not try to mutate the data source on its own.
Wiring the Tap Back to the Controller
The table view controller configures the cell and decides what to do with the tap. A useful trick is to capture a stable model identifier instead of the current row number, because rows can move when you insert, delete, or sort.
This is easier to maintain than making the cell climb the view hierarchy or guess its own index path.
Delegate Pattern Versus Closure Pattern
Closures are compact and work well when the cell only has one or two actions. A delegate protocol is better when the cell needs to report several events or when your team prefers a more explicit contract.
The design rule is the same either way:
- the cell owns the button
- the controller owns the data
- the event travels from the cell back to the controller
Once you preserve that direction, reuse bugs become much less common.
Why button.tag Is Fragile
A common shortcut is setting button.tag = indexPath.row and reading that tag later. It can appear to work, but it breaks easily when rows are inserted, deleted, filtered, or reordered. The tag is just an integer snapshot from configuration time, not a durable connection to the current model.
Another fragile approach is walking up superview references until you find a cell. That depends on UIKit view hierarchy details you do not control. It also makes the code harder to read than a direct callback.
If You Need the Current Index Path
Sometimes the action truly depends on the cell's current table position. In that case, ask the table view for the index path when the tap arrives:
That pattern is still secondary to model identifiers, but it is safer than storing a row number in a tag and assuming it will remain correct forever.
Common Pitfalls
- Using
button.tagas if it were a stable model identifier. - Letting the cell update controller-owned data directly.
- Forgetting to clear callbacks in
prepareForReuse. - Creating a retain cycle by capturing the controller strongly inside the callback.
- Walking the superview chain instead of passing the event back cleanly.
Summary
- The clean solution is to let the cell report the tap and let the controller handle the data change.
- Closures are a compact option; delegates are a good fit for more complex cells.
- Prefer model identifiers over row numbers because table rows can move.
- Reuse matters, so reset callbacks in
prepareForReuse. - Avoid tags and superview-walking when a direct callback pattern is available.
Related reading
- Get class name of object as string in Swift
- Get content uri from file path in android
- Get currency symbols from currency code with swift
- Get current date in Swift 3?
- Get current scroll position of ScrollView in React Native
- Get current URL of UIWebView
- Get day of week using NSDate
- Get device location only country in 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.