0

I created custom cells for my table. Basically my cells have UIButton on them. I am reusing those cells.

However i have trouble with those buttons, because when cells is reused then all it's elements also is reused but i am seeking that these Button will be unique to every cells.

Maybe someone could propose a solution for this functionality ?

Streetboy
  • 4,351
  • 12
  • 56
  • 101
  • To know which row a button was in when it was pressed: http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – jrturton Oct 23 '12 at 06:27
  • @jrturton: that debugger variable completion popup keeps stalking me! We need to find out how to make this happen. – NSTJ Oct 23 '12 at 07:53

2 Answers2

0

How about this if you have a property button on your custom cell?

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    static NSString *CellIdentifier = @"My Cell Identifier";
        MyCustomCellClass *cell = (MyCustomCellClass *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MyCustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        // Configure the cell

        UIButton *myButtonForThisCell = [MyButtonArray objectAtIndex:indexPath.row];

        cell.button = myButtonForThisCell;
        return cell;
    }               
NSTJ
  • 3,858
  • 2
  • 27
  • 34
0

create a buttonView UIVIEW file with two methods as

-(NSInteger) getGridIndex
{
    return gridIndex;
}

-(void) setGridIndex:(NSInteger)value
{ 
    gridIndex = value;
}

Now in cellforrowatindexpath method

        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.accessoryType = UITableViewCellAccessoryNone;

        ButtonView *buttonView=[[ButtonView alloc] initWithFrame:CGRectMake(110, 110, 160, 26)];
                buttonView.tag=18;
         UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(55, 0, 60, 26)];
[button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
         [buttonView addSubview:button];
          [cell addSubview:buttonView];
        }
     ButtonView *buttonView=(ButtonView*)[cell viewWithTag:18];
     NSArray *d=[buttonView subviews];
      UIButton *button=[d objectAtIndex:0];
    //here you can change the setting or title of button or whatever u want to do.
      [buttonView setGridIndex:indexPath.row];
    }

Now in button action :

-(void)ButtonAction:(UIButton*)sender{
    ButtonView *view = (ButtonView *)[sender superview];
    NSInteger n=[view getGridIndex];
    //here n is the indexpath.row at which the button was tapped..you can write its action accordingly.
}