0

i am creating button dynamically. and creation action methods by

  [newButton addTarget:self action:@selector(goToNew:)forControlEvents:UIControlEventTouchUpInside];

i want to send argument (indexpath.row) from tableview , but not want to use tag.. because i need that tag will remain same for all the buttons , how can i pass argument in button action ?

actually i am adding button in each tableview cell , and i want action for all those buttons , but if i use tag = indexpath.row and and use it with action, it works but problem of overlaying button happen.hence i want tag would be constant.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UIButton *btn;

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(goToNew:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 55;
    [cell.contentView addSubview:btn];

}
else {
    btn = (id)[cell.contentView viewWithTag:55];

}

return cell;

}

- (void) goToNew:(id)sender   

{
UIButton *b = (UIButton *)sender;
UITableViewCell cell = (UITableViewCell)[b superview];
int row = [msgTableView indexPathForCell:cell].row;
(@"row is :::%d",row);

}
PJR
  • 13,052
  • 13
  • 64
  • 104

3 Answers3

1

By doing following code, you will get the indexPath.row and no need to set the button tag.

- (IBAction)goToNew:(id)sender 
{
    UIButton *btn = (UIButton*) sender;
    UITableViewCell *cell = (UITableViewCell*) [btn superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    int row = indexPath.row;
}
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
  • but how can i know that which button it is ... means in which cell my button is , i get it with sender.tag but now how can i get that integer value ? – PJR Sep 08 '11 at 12:20
  • using btn.tag you can get tag of taht button – Narayana Rao Routhu Sep 08 '11 at 12:26
  • i want to do it without tag.. i want tag will remain constant. – PJR Sep 08 '11 at 12:29
  • 1
    @PJR, In the above code, use `NSIndexPath *indexPath = [tableView indexPathForCell:cell]; int row = indexPath.row;` – EmptyStack Sep 08 '11 at 12:33
  • actually i am adding button in each tableview cell , and i want action for all those buttons , but if i use tag = indexpath.row and and use it with action, it works but problem of overlaying button happen.hence i want tag would be constant. – PJR Sep 08 '11 at 12:44
  • 1
    If you are adding the button to `cell.contentView`, you should retrieve it as, `UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;` – EmptyStack Sep 08 '11 at 12:48
1

You can pass another argument as set title of button using different forstate.Here you can pass indexpath as title of button.like

    btn.tag=10;
    [btn setTitle:[NSString stringWithFormat:@"%d",indexpath.row] forState:UIControlStateDisabled];
    [btn addTarget:self action:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

and you will received argument as

-(void)btnClick:(id)sender
{
UIButton* b = (UIButton*) sender;   
NSLog(@"tag=%d",b.tag);
   //Below line will got indexpath in string formate..
NSLog(@"title =%@",[b titleForState:UIControlStateDisabled]);
}
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
0
-(void)goToNew:(id)sender

{
    UIButton *btnTemp = (UIButton *)[yourTableView viewWithTag:sender];
    int iTag = btnTemp.tag;  

}  
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • for this when i create my button i have to spectifiy mybtn.tag = indexpath.row , am i right ? but i want that tag would be constant , so i want to do without specifying a tag at the creation of buttom – PJR Sep 08 '11 at 12:27
  • there is no title for button. only image. how can i use it ? – PJR Sep 08 '11 at 12:41
  • 1
    i think you should use tag so you can differentiate buttons... ot you can use row's index so you can get at which row's button gets clicked... – Maulik Sep 08 '11 at 12:43
  • actually i am adding button in each tableview cell , and i want action for all those buttons , but if i use tag = indexpath.row and and use it with action, it works but problem of overlaying button happen.hence i want tag would be constant. – PJR Sep 08 '11 at 12:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3269/discussion-between-maulik-and-pjr) – Maulik Sep 08 '11 at 12:47