Strange things happening to my table view

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.


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

    - (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(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;


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.

[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.

Need Your Help

Calling Cocoa IBAction from Carbon Code

cocoa carbon ibaction

I'm trying to call a Cocoa IBAction from Carbon code...

VP8 Encoding results in grayscale image on Google Glass

android google-glass vp8 libvpx

The application I am working on is developed for Google Glass but runs on Android tablets as well.It uses VP8 encoding to transfer camera images to a remote application.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.