Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a button press from a custom UITableViewCell's button

I have a project with a custom UITableViewCell (cell.h/.m/.xib) that has 2 labels (labelMain/labelSub), and 2 buttons on it (buttonName/buttonInfo) linked to 2 actions (showName/showInfo).

What I want to do is be able to access the 2 actions in my projects main viewcontroller so that when showName is pressed, a textfield.text in the viewcontroller (not in the cell) is set to that specific cell's labelMain.text.

Hope that makes sense. My problem is that if I write the action (showName) in the cell.m, I cannot access the textfield from my main viewcontroller. On the flip side, if I write the action in my viewcontroller, how do I know which button inside which cell was tapped?

Hope this makes sense...

like image 948
James V Avatar asked Mar 10 '12 03:03

James V


2 Answers

Use tag can identify which button in which cell is being tapped.

- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      //init identifier
      if (cell ==nil)
       {
        //load nib file
       }
    
      buttonName.tag = indexPath.row;
      [buttonName addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchUpInside];

      buttonInfo.tag = indexPath.row;
      [buttonInfo addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];

    }
}

-(void) showName:(UIButton*)button{
  int row = button.tag; //you know that which row button is tapped
}

-(void)showInfo:(UIButton*)button{
 int row = button.tag;//you know that which row button is tapped
}

---------------- EDIT -------------

If you need to know which button is pressed based on row & section, you may try this below. (in cellForRowAtIndexPath: method)

int tag = (indexPath.row+1)+(indexPath.section*100);
buttonName.tag = tag;

When button at

row = 5, section = 0 then tag = 6.

row = 4, section = 3 then tag = 305.

row = 0, section = 11 then tag =1101.

limitation, row cannot be more than 99. and DON'T use positive tag for other view. if you need use tag, try negative. (-1, -9, -100).

so from here, you can calculate back row & section of indexPath. using this :

-(NSIndexPath*)getIndexPathFromTag:(NSInteger)tag{
    /* To get indexPath from textfeidl tag,
     TextField tag set = (indexPath.row +1) + (indexPath.section*100) */
    int row =0;
    int section =0;
    for (int i =100; i<tag; i=i+100) {
        section++;
    }
    row = tag - (section*100);
    row-=1;
    return  [NSIndexPath indexPathForRow:row inSection:section];
    
}

how to use :

-(void)showInfo:(UIButton*)button{
     NSIndexPath *indexPath = [self getIndexPathFromTag:button.tag];
     int row = indexPath.row;
     int section = indexPath.section;
}
like image 123
HelmiB Avatar answered Sep 27 '22 16:09

HelmiB


Try this in viewController.m file in cellForRowAtIndexPath method

[[customCell showNameBtn] setTag:indexPath.row];
[[customCell showNameBtn] addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchDown];

-(void) showName:(id)sender
{
// check here the sender tag and then perform you action what you want.
}
like image 33
hchouhan02 Avatar answered Sep 27 '22 17:09

hchouhan02