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

  • Home
  • SEARCH
  • 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 8171865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:35:49+00:00 2026-06-06T21:35:49+00:00

I am trying to add a button to a cell in my cellForRowAtIndexPath method.

  • 0

I am trying to add a button to a cell in my cellForRowAtIndexPath method. That works but I seem to be adding a button every time the cell is updated (discovered this because I have updated the wrong screen position and I have 2 buttons that both work and can be clicked).

When is the correct time to allocate and release a UIButton thats added to a cell? Surely I cannot release the button at the end of cellForRowAtIndexPath – that would destroy the button object wouldn’t it? All of the snippets I’ve found with buttons added to cells so far seem to keep instantiation new UIButton objects within cellForRowAtIndexPath which has me somewhat confused..

  • 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-06T21:35:51+00:00Added an answer on June 6, 2026 at 9:35 pm

    A couple of things:

    1. Buttons are appearing elsewhere because you’re using the same cell identifier for different types of cells. Each type of cell should get it’s own identifier.

    2. In terms of the button disappearing, I don’t know why that’s happening as a result of reloadRowsAtIndexPaths, but if you move the button to the front of the other subviews, you won’t lose it.

    3. You probably want to autorelease your UITextField. (Same true with NSString in titleForHeaderInSection.)

    Anyway, the cellForRowAtIndexPath becomes something like:

    #define kTextFieldTag 100
    #define kButtonFieldTag 101
    
    //-----------------------------------------------------------------------------------------
    // cellForRowAtIndexPath
    //-----------------------------------------------------------------------------------------
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier;
        UITableViewCell *cell;
    
        // Configure the cell...
        switch( [indexPath section] ) 
        {
            case WORKSHEET_SECTION_CLIENT: 
                cellIdentifier = @"CellClient";
    
                cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
                if (cell == nil) 
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
                }
    
                cell.textLabel.text = clientName;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;      
                break;
    
            case WORKSHEET_SECTION_PERIOD: 
            {
                UIButton *myButton1;
    
                cellIdentifier = @"CellPeriod";
    
                cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
                if (cell == nil) 
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    
                    // Don't need UIButton alloc because method below does it for us..
                    myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    myButton1.tag = kButtonFieldTag;
    
                    CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath];
    
                    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];  
    
                    [cell.contentView addSubview:myButton1];      
                }
                else
                {
                    myButton1 = (UIButton *)[[cell contentView] viewWithTag:kButtonFieldTag];
                }
    
                cell.textLabel.text = timeElapsed;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
                [[cell contentView] bringSubviewToFront:myButton1];
    
                break;      
            }
    
            case 2:
                cellIdentifier = @"Cell2";
    
                cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
                if (cell == nil) 
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
                }
    
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"Amount $130";
                break;      
    
            case 3:
            {
                cellIdentifier = @"Cell3";
    
                cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                UITextField *myTextField;            
    
                if (cell == nil) 
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    
                    myTextField = [[[UITextField alloc] initWithFrame:CGRectMake(20,10,125,25)] autorelease];
                    [myTextField setTag:kTextFieldTag];
                    myTextField.adjustsFontSizeToFitWidth = NO;
                    myTextField.backgroundColor = [UIColor clearColor];
                    myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
                    myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
                    myTextField.textAlignment   = UITextAlignmentLeft;
                    myTextField.keyboardType    = UIKeyboardTypeDefault;
                    myTextField.returnKeyType   = UIReturnKeyDone;
                    myTextField.clearButtonMode = UITextFieldViewModeNever;
                    //      myTextField.delegate = self;
                    myTextField.font = [UIFont systemFontOfSize:14];
    
                    [cell addSubview:myTextField];
                }
                else
                {
                    myTextField = (UITextField *)[[cell contentView] viewWithTag:kTextFieldTag];
                }
    
                // note, theoretically, you could blow away anything that the user typed here
                [myTextField setPlaceholder:@"Type Data Here"];          
    
                cell.accessoryType = UITableViewCellAccessoryNone;
    
                cell.textLabel.font = [UIFont systemFontOfSize:14];
                cell.textLabel.numberOfLines = 0;
                break;
    
            }
    
            default:
                cellIdentifier = @"CellDummy";
    
                cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
                if (cell == nil) 
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
                }
    
                cell.textLabel.text = @"Dummy";
                break;
    
        }
    
        return cell;
    }
    

    Personally, I might split this into separate methods, but that’s up to you.

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

Sidebar

Related Questions

I'm trying to add two buttons to each cell in a tableview, but when
I am trying to add button into my table cell like below: - (UITableViewCell
I'm trying to add an AirPlay button to my app and used the following
I am trying to add a pinterest button to a control. I am trying
I'm trying to add a custom button to the button panel of jQuery's datepicker.
I am trying to add a transparent button bars at the top and bottom
I am trying to add bootstrap drop down button in a column of jqGrid
I am trying to add Become-a-Fan button to my 2008 asp.net website (vb) Here
I'm trying to add the Twitter Share button to my website. I've gone to
I have been trying to add an askquestion dialog box to a delete button

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.