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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

okay, so should i remove this line? i need it to display 1 as soon as the cell is created and i cant seem to do that either. Is there another place i could put that line? Thank you for the reply!!
@iProRage - It's a little hard to tell what your intent is based on just the code you've posted. From what I see, this will not update the cell you touched's value, but the value of the cell at the index the value gets added to number at. If you intend to change the value of the cell that was touched, you need to create your number array beforehand, with @"1" as the initial value for each cell's index, then change the value at the correct index for the cell that was touched.
i am just trying to add and subtract 1 to the cell. I did this before without adding the value to an array and when i scrolled a cell out of the view and when it returned, the value was all messed up. And now im adding it to an array, because i read that it will fix my problem. And where should i set the initial value of 1 in the array at? The cellForRow method, or the viewDidLoad? Thanks again doug!!
No problem dude. Okay, I think I understand what you're trying to do. I'll update my answer to address that. You may also want to take a look at using properties. It looks like you're accessing all of your class variables directly, and managing them directly. From what I see, it's probably creating some memory leaks.
In your addLabelText method you're calling this every time number = [[NSMutableArray alloc]initWithObjects:num, nil]. That will cause leaks because you aren't releasing the previously allocated NSMutableArray. I'd highly recommend you read through Apple's tutorials memeory management if you haven't--well worth the read. Cheers!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.