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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:23:26+00:00 2026-05-28T17:23:26+00:00

i have a UItableView and i have two buttons on each cell. You can

  • 0

i have a UItableView and i have two buttons on each cell. You can add or subtract 1 from the cell‘s textLabel. I add the cells current value +1 with this:

  - (IBAction)addLabelText:(id)sender{
     num = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];//<--- num is an NSNumber
     number = [[NSMutableArray alloc]initWithObjects:num, nil];//<---- number is an NSMutableArray
     [myTableView reloadData];

}

and I am trying to subtract the text and store it in an array with this:

    - (IBAction)subtractLabelText:(id)sender
{
        if ( [[cell.textLabel text] intValue] == 0){     


     num = [NSString stringWithFormat:@"%d",[num intValue] +0];
     [number addObject:num];
         [myTableView reloadData];

     }
     else{
     num = [NSString stringWithFormat:@"%d",[num intValue] -1];
     [number addObject:num];
     [myTableView reloadData];
     }
}

and im trying to set the cell.textLabel.text in the cellForRow like this:

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

    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
     {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];


     } 
    cell.textLabel.text = [number objectAtIndex:indexPath.row];//<---IM USING THIS LINE TO SET THE NEW TEXTLABEL
    cell.textLabel.text = @"1";


return cell;
}

MY PROBLEM

So, the addition works, but the subtraction does not. It doesnt work at all when i press the button on the cell. Thanks in advance!!

  • 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-28T17:23:26+00:00Added an answer on May 28, 2026 at 5:23 pm

    At the risk of pointing out the obvious… you seem to have hard-coded the text property to @”1″.

    cell.textLabel.text = [number objectAtIndex:indexPath.row];//<---IM USING THIS LINE TO SET THE NEW TEXTLABEL
    cell.textLabel.text = @"1";
    

    The first line is probably doing what you think… but then you’re immediately changing it back to @”1″.

    EDIT – Based on clarification in comments, here’s what I think you want to do. I will modify your own code as posted.

    Note that I put my addition into viewDidLoad as an example. You could do this in init, or any number of other places, at whatever point you know how many cells you want to show.

    - (void)viewDidLoad {
        self.number = [[[NSMutableArray alloc] init] autorelease];
        for (int i = 0; i < [HOW MANY CELLS DO YOU WANT?]; i++) {
            [self.number addObject:@"1"];
        }
    
        [myTableView reloadData];
    }
    
    - (IBAction)addLabelText:(id)sender {
    // Note that I'm assuming here that your button is a direct child of the cell.
    // If not, you'll need to change this.
        UITableViewCell *cell = [sender superview];
        NSInteger newNumber = [cell.textLabel.text intValue] + 1;
        NSString *newNumberString = [NSString stringWithFormat:@"%d", newNumber];
    
        [self.number replaceObjectAtIndex:cell.tag withObject:newNumberString];
    
        [myTableView reloadData];
    }
    
    - (IBAction)subtractLabelText:(id)sender {
    // Note that I'm assuming here that your button is a direct child of the cell.
    // If not, you'll need to change this.
        UITableViewCell *cell = [sender superview];
        NSInteger newNumber = [cell.textLabel.text intValue] - 1;
        newNumber = (newNumber < 0) ? 0 : newNumber;
    
        NSString *newNumberString = [NSString stringWithFormat:@"%d", newNumber];
    
        [self.number replaceObjectAtIndex:cell.tag withObject:newNumberString];
    
        [myTableView reloadData];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *identifier = @"Cell";
    
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        }
    
        cell.tag = indexPath.row; // Important for identifying the cell easily later
        cell.textLabel.text = [self.number objectAtIndex:indexPath.row];
    
        return cell;
    }
    

    I don’t want to get too much more in depth, but I would recommend you take a look at reloading only the cell modified, instead of calling [myTableView reloadData] every time.

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

Sidebar

Related Questions

I have a UITableView and i programatically add two buttons to the cell. 1
I have a UITableView with two custom cells and each of them has a
I have a UITableView with rows that each have two actions that can be
i have a table view. And im adding two buttons to each cell: -
I have a UITableView and i can add and delete cells. I also have
Ok i have a table view that can add and delete cells. For each
I have a UITableView with two sections: free and paid. Each section has a
In my IPhone app I have a UITableView. I want two buttons to appear
I have two buttons inside sectioned tableview cell thumbs up and thumbs down. Initially
I have two buttons inside sectioned tableview cell thumbs up and thumbs down. Initially

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.