views:

94

answers:

1

Hi all,

In my application I'm doing dynamic resizing of cells and label in it depending upon the text in it. I'm adding button to cells in uitableview.

I'm taking the label instance and button instance in a new label and button variable respectively and setting their frames to arrange them properly after resizing.

     if(cel==nil)
     {
       //some code
       original_label=[[UILabel alloc]init];
       original_label.tag=111;

       //SOME MORE CODE GOES HERE

       original_button=[[UIButton alloc]init];
       original_button.tag=222;
       //SOME MORE CODE GOES HERE
     }

      new_label=(UILabel *) [cell viewWithTag:111];    //This' how I'm taking the label instance on cell and below button instance on cell in new variables
      new_button = (UIButton * ) [cell viewWithTag:222];

Earlier I kept the tags of all the buttons on cells same, so it was easier to get button instances on cells properly and were being arranged properly. But now I want to recognize these buttons separately as I'm adding some functionality on button_click. I'm giving the buttons that are added to the cells incremental tags[1,2,3...9 and so on]. Now, how can I take these button tags in some range like[suppose 1-9]?

Can anybody help?

Thanks in advance.

+1  A: 

You can keep the button tags the same as you had before.

Instead, in the button_click method, figure out which row the button is in like this:

- (void)button_click:(UIButton *)button
{
    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

    //code to handle this indexPath.section and indexPath.row...
}

This assumes you have added the button to cell.contentView which is what the first superview gets. The second superview gets the cell.

The addTarget for the button should look like this (note colon after button_click):

[original_button addTarget:self action:@selector(button_click:) forControlEvents:UIControlEventTouchUpInside];
DyingCactus
Awesome!!!... Thank you so much DyingCactus... Exactly what I was looking for...
neha