Take the 2-minute tour ×

I have a table view. I originally used the cell's textLabel to handle this, but that didnt work, so i added a UILabel to each cell programatically and i have two buttons a add button and a subtract button which basically counts up or down when pressed. (goes up or down by 1) The methods for the two buttons are:

 - (IBAction)addLabelText:(id)sender{    
   cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] +1];


- (IBAction)subtractLabelText:(id)sender
    if ( [[cellLabel text] intValue] == 0){ 
        cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] +0];
        cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] -1];

when i pressed the buttons, and added multiple cells, the buttons interfered with each other's labels. So i added this line of code to each of the above methods. :

    cellLabel = (UILabel*)[sender superview];    

in hopes that it would fix the problem. But it actually added i believe the cells textLabel again. Here is a photo of what it looks like before and after i press one of the buttons with the new line added.

enter image description here


enter image description here

any help with fixing the buttons interfering with each other cells UILabel's please help!! :D Thanks

    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(280,10,25,55)];
         [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(250,10,25,55)];
         [subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
         [subBtn setTitle:@"+" forState:UIControlStateNormal];
         [subBtn setEnabled:YES];
         [cell addSubview:subBtn];

         cellLabel = [[UILabel alloc]init];
         [cellLabel setFrame:CGRectMake(85, 22, 27, 27)];
         [cellLabel setText:@"1"];
         [cell addSubview:cellLabel];**
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];    
   // cell.textLabel.text = [cells objectAtIndex:indexPath.row];
    **if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle5.png"]]) {
        [cellLabel setFrame:CGRectMake (195, 22, 30, 30)];
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle4.png"]]) {
        [cellLabel setFrame:CGRectMake (170, 22, 30, 30)];
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle3.png"]]) {
        [cellLabel setFrame:CGRectMake (140, 22, 30, 30)];
    if ([cell.imageView.image  isEqual:[UIImage imageNamed:@"paddle2.png"]]) {
        [cellLabel setFrame:CGRectMake (110, 22, 30, 30)];
return cell;
share|improve this question
If you're building for iOS5, you might want to look into using a UIStepper instead of the two UIButtons. –  Mark Leonard Nov 30 '11 at 4:37

2 Answers 2

up vote 1 down vote accepted

set tag to cell whet you cnfigurating it: in my table I have some sections and some cells in every section. So, cells from first (0) section has tag 0...9999. from second - 10000...19999 adn so on. I mean tag of cell = 10000*number of section PLUS number of raw of this cell in ths section. then, to calculate what exacly is ths cell, I see how many 10000 tag has, to calculate from what sectiont it is, and how many >10000 tag has to calculate from what raw it is. So, I have section and raw from my table view.

cell.contentView.tag = indexPath.row+10000*indexPath.section; //or any you like
[cell.contentView addSubview:button]; //adding your button

then, in button's callback:

int row=0;
int section=0;
long tag = [sender superview].tag;
NSLog(@"tag: %ld", tag);
for (int i = 0; i<[data count]; i++) {
    if (tag>=10000) 
    else i = [data count];
row = tag;

NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:section];
indexPath = [indexPath indexPathByAddingIndex:row];
[tableView cellForRowAtIndexPath:indexPath];

now, you have full access to your cell. use calculated raw and section ta get access to cell.

share|improve this answer
okay, i did. thanks for the help!:D –  iProRage Nov 30 '11 at 4:11
I adited my answer. try this. –  SentineL Nov 30 '11 at 4:18
okay, shall i put this in the subtractLabelText method? Also, i copied and pasted your code and i am getting an error complaining about one of the ] brackets and i cannot figure it out! :/ –  iProRage Nov 30 '11 at 4:26
yes, this code is for your subtractLabelText and addLabelText and sorry, i fixed it. Replace "-1" –  SentineL Nov 30 '11 at 4:32
ok thank you very much for the help! i switched out your UILabel*label line to my already created UILabel cellLabel however, when i press the button, the app crashes. And i read the console and it said: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 4]' Thanks again!:D –  iProRage Nov 30 '11 at 4:36

[sender superview] returns superview of your UIButton, which is UITableViewCell that also have text property so that's why your code still running fine even you casted it incorrectly.

Show us how you construct and add cellLabel so we can see what's wrong with it.

share|improve this answer
okay, thanks for the help! I will post it in my question! –  iProRage Nov 30 '11 at 4:06

Your Answer


