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 6860495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:24:57+00:00 2026-05-27T02:24:57+00:00

I have a UITableView and i programatically add two buttons to the cell. 1

  • 0

I have a UITableView and i programatically add two buttons to the cell. 1 button adds to the cells text ( counts up ) the other subtracts 1 (counts down). However, lets say i add 4, the cell’s text will be 4, but when i scroll that cell up and out of the view, when it comes back down into view, the cells text is back to 1 which is where it started out. The same happens if i add(it does the same if i subtract also) to the cells text and switch pages and then go back to the table view. Here is the cellForRow:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         newBtn = [[UIButton alloc]init];
         newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [newBtn setFrame:CGRectMake(260,20,55,35)];
         [newBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [newBtn setTitle:@"-" forState:UIControlStateNormal];
         [newBtn setEnabled:YES];
         [cell addSubview:newBtn];

         subBtn = [[UIButton alloc]init];
         subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
         [subBtn setFrame:CGRectMake(200,20,55,35)];
         [subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [subBtn setTitle:@"+" forState:UIControlStateNormal];
         [subBtn setEnabled:YES];
         [cell addSubview:subBtn];
    } 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];    
    cell.textLabel.text = [cells objectAtIndex:indexPath.row];

return cell;
}

Any and all help is appreciated! Thanks:D

Here is the screen Shot of the cell

Methods for buttons

    - (IBAction)addLabelText:(id)sender{    
    cell = (UITableViewCell*)[sender superview];    
    cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];
}  

- (IBAction)subtractLabelText:(id)sender
{
    cell = (UITableViewCell*)[sender superview];        
    if ( [[cell.textLabel text] intValue] == 0){ 
        cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +0];
    }
    else{
        cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] -1];
        //[myTableView reloadData];

    }
}
  • 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-05-27T02:24:57+00:00Added an answer on May 27, 2026 at 2:24 am

    You need something like this, where you are storing the values outside the cells. This is because the cells get reused and are not good long term storage.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
         {
    
             subBtn = [[UIButton alloc]init];
             subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
             [subBtn setFrame:CGRectMake(200,20,55,35)];
             [subBtn addTarget:self action:@selector(addLabelText:indexPath.row) forControlEvents:UIControlEventTouchUpInside];
             [subBtn setTitle:@"+" forState:UIControlStateNormal];
             [subBtn setEnabled:YES];
             [cell addSubview:subBtn];
        } 
    
        // we're loading the value from the array each time the cell is displayed.
        cell.textLabel.text = [cellLabelValues objectAtIndex:indexPath.row];
    
    return cell;
    }
    
     - (IBAction)addLabelText:(int)currentRow{    
         NSString *newValue = [NSString stringWithFormat:@"%d",[[[cellLabelValues objectAtIndex:currentRow] intValue] +1];
        // we update the value in the array since this is the source of the data for the cell
        [cellLabelValues replaceObjectAtIndex:currentRow withObject:newValue];
        // now reload to get the new value
        [myTableView reloadData];
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a uitableview with 2 cells (custom tableviewcells) and a button for login.
I have created a UITableView with two cells, username and password using the code
I have UITableView with very large cells with lots of content (more than one
I have a UITableView cell that is going to have a variable size depending
I have a UITableView which is populated by an array, I have a button
I have a UITableView that displays single large images in each cell. The names
I have a uitableview that loads fairly large images in each cell and the
I have a UITableView with an Index on the side; I want to add
I have a UITableView of custom UITableViewCells which looks like this: [ CELL 0
I have UITableView and it add data from NSMutableArray. So, I want to know

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.