Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:08:04+00:00 2026-06-07T01:08:04+00:00

I understand how to get button clicks if I have a button within a

  • 0

I understand how to get button clicks if I have a button within a row of UITableView but what I cannot figure out is the correct way to add a button to a UITableViewCell subclass and get the clicks for it within my UITableView class. I think I’m making the mistake of putting everything into the layoutSubviews method. Can somebody please point me in the right direction?

-(void) layoutSubviews 
{
//  UIButton *myButton1; <- This is declared as a property in my subclass header file

  [super layoutSubviews];

  //self.textLabel.frame = CGRectMake(0, 0, 40, 20);

  //        cell.textLabel.text = timeElapsed;
  self.textLabel.frame = CGRectMake( 5, 5, 80, self.frame.size.height - 10 );
  self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   

  // Don't need UIButton alloc because method below does it for us..
  myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

  //CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath];
  CGRect cellFrame = self.frame;

  myButton1.tag = idButton;

  int nButtonWidth  = 80;
  int nButtonHeight = 30;
  int nActualCellWidth = cellFrame.size.width - 40;
  int nButtonDeltaX = ( nActualCellWidth      - nButtonWidth  ) / 2;
  int nButtonDeltaY = ( cellFrame.size.height - nButtonHeight ) / 2;
  myButton1.frame = CGRectMake( nButtonDeltaX, nButtonDeltaY, nButtonWidth, nButtonHeight );      

  [myButton1 setTitle:@"OK" forState:UIControlStateNormal];

  // PDS: Add delaget for stopwatch clicking..
//  [myButton1 addTarget:self action:@selector(buttonClickedStopWatch:) forControlEvents:UIControlEventTouchUpInside];  

  [self.contentView addSubview:myButton1];      
  [self.contentView bringSubviewToFront:myButton1];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:08:06+00:00Added an answer on June 7, 2026 at 1:08 am

    First of all, layoutSubviews may be called multiple times so that’s really not a good place to create a button and add it to the view hierarchy. This will cause a new button to be created each time. You should rather create the button in your designated initializer and only use layoutSubviews for setting frames and such.

    Second, an instance of UITableView is not usually a good place to handle button clicks. That’s commonly a task for a subclass of UIViewController.

    Whichever class you select for handling the tap action, start by adding a property in the cell for your custom button. Then let the UITableView or UIViewController add the action target for that control event by accessing the property and setting target to self.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get that asterix is for pointers, and I understand about pointers now. But
I understand PCRE pretty well, but MySQL's regular expression flavor seems to get the
I have some JQuery where when you click on a Add to Basket button
I have a button, and when the user clicks on it, the camera opens
I been having trouble trying to figure this out. When I think I have
I'm trying to understand how to get librsync to work (ie. make signature/make delta/patch)
My main problem is I don't understand how to get the position of the
i understand that some files get published to this folder in my application. i
I cant understand what this warning I get on Xcode is about. Searching for
I'm trying to understand why my code get stuck in the for loop of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.