How to receive UIButton tap events placed in multiple UITableViewCell lines in a higher class (ViewController)

Asked 2 months ago, Updated 2 months ago, 6 views

I'd like to know how to use Table View.

when multiple buttons are placed in a single cell for the following sources: Is it possible to determine which buttons are placed in the higher class (such as ViewController)?
I don't know how to do it, so I'm stuck.Please let me know.


classViewController:UITableViewController {
 // Method for configuring the contents of a Cell
  override functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        // [Attention]
        letcell=tableView.dequeueReusableCell(withIdentifier: "testcell", for:indexPath) as!myCusutomCell
        return cell

   // I want to receive tap information for multiple buttons in myCusutomCell here (in ViewController)


// testcell
class myCusutomCell:UITableViewCell{



swift tableview

2022-09-30 11:27

1 Answers

Let me introduce you to two methods.First, use the responder chain.

If you have experience developing Mac applications, the responder chain is a common method for implementing menu bar menus, but it's not easy to see in iOS application development.However, it is a mechanism that is active behind the scenes, such as the propagation of touch events ( functouchesBegan(_touches:Set<, with event:UIEvent?)), so you must learn and understand well.
A responder chain is a mechanism that passes events caused by users, etc., one after another, between objects of UIResponder, which are linked by multiple chains, as the direct translation of the "response chain" is well described.If any object in the chain has an action method (@IBAction) implemented to handle the event, it executes the action method.
Did you notice that there is an item called "First Responder" in the View Controller on the Edit Storyboard screen?This means the tip of the responder chain.

Enter a description of the image here

Connecting the First Responder icon from a UIControl object, such as a button, allows you to send an instruction (Action) to the responder chain instead of to a specific object.
First, view the First Responder Attributes Inspector and register the new action.In the figure below, we have registered the action doSomething."Type" is NSObject for versatility.

Enter a description of the image here

When you connect (Connect) to First Responder from a button object, you will find the action doSomething that you just registered in the pull-down menu and select it.

Enter a description of the image here

You are now finished editing the Storyboard.Next, implement an action method for receiving events in the ViewController class.

class ViewController:UITableViewController {

    override func viewDidLoad(){

    @ IBAction func doSomething(_sender:Any){


With the above edits, tapping the button on the table cell will output "Hello".
UIViewController subclasses are automatically positioned closer to the end of the responder chain, so you can receive almost any event.

Use the Instance Method addTarget(_:action:for:) class method to connect to the First Responder in a program instead of the Storyboard.Note that the argument target for this method is optional (Any?) and see the reference description.

target The target object —that is, the object which action method is
called.If you specify nil, UIKit searches the responder chain for an
object that responses to the specified action message and deliveries the
message to that object.

After fIf you specify nil,IK "UIKit finds an object in the responder chain that responds to a selector (action:) and sends an action to that object" is a brief description of the responder chain.

Enter a description of the image here

The Table View under the UITableViewController can be in the mode Static Cells.As the name implies, cells are statically placed without the need for a mechanism to dynamically reuse cells, but this mode has the advantage of connecting buttons placed on cells (UIControl) to action methods defined in the UITableViewController subclass.It's a great advantage to just edit on the Storyboard without thinking, "Buttons are on the cell..."

2022-09-30 11:27

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.