2

I have UItableViewCell created with a nib file, and have put in my cell a button. i hace created an IBAction to associate an action when the the user click the button.but i have a crash and i don't know what it is the problem, i have set all the things necessary.

//in my .h
-(IBAction)go; 

//in my .m
-(IBAction)go 
{ 
    NSLog(@"hello");
} 

but i have a crash and nothing is show in the consol debug.

how can i set a button in UITableviewcell and associate with this button an action. thanks for your answer

Kheldar
  • 5,361
  • 3
  • 34
  • 63
izan
  • 727
  • 2
  • 11
  • 22
  • Are you sure that `NSLog@("hello");` isn't the problem? It should be `NSLog@(@"hello");` – Nick Weaver May 03 '11 at 13:37
  • No, it's not the problem. I think that add a button to a custum cell is not the same thing to add a button in a UIView, Can any one give me the method how we can add a button ( with an action) to a custum cell. – izan May 03 '11 at 13:52
  • "set all the things as necessary" is a very imprecise formulation... By definition, if that were true, you'd not have a bug, would you? – Kheldar Feb 06 '12 at 12:54
  • possible duplicate of [How to know the UITableview row number](http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number) – jrturton Nov 11 '13 at 14:11

7 Answers7

10

In my opinion setting the tag is a poor way. It restricts better usage for the Tag elsewhere in the application.

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];

Using this approach you can actually retrieve the IndexPath for the cell.

Tim
  • 8,932
  • 4
  • 43
  • 64
1

This is what worked for me...

- (void)yourButton:(id)sender{

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    CustomCellClass *cell = (CustomCellClass *)[self.tableView cellForRowAtIndexPath:indexPath];

    NSString *myFooText = cell.cellLabelFoo.text;
}
Dustin
  • 213
  • 2
  • 5
1

Create a button programmatically and add it to the sub view of the cell. Set tag as the indexPath.row value and add target to the button. Now in the target you could simply use :

 -(void)buttonInsideCellIsPressed : (id)sender{
   UIButton *button = (UIButton *)sender; 
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
   CustomCell *cell = (CustomCell*)[myTable cellForRowAtIndexPath:indexPath];

   // Here you may change the background image for the button for rollover or anything 
    }
Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • 1
    Please see my answer for a more suitable, maintainable way as a solution. My approach actually retrieves the IndexPath of the cell pressed. – Tim Feb 06 '12 at 12:53
  • What is the problem here. It works fine with me. – Sandeep Oct 10 '12 at 07:37
  • It is giving error: unknown CutomCell and wrong spelt UItableViewCell moreover its not accepting this code. – iPhone Programmatically Oct 10 '12 at 07:48
  • Well it depends upon your design, your custom cell class and the implementation. Could you post the code so that I could have a look at it. – Sandeep Oct 10 '12 at 08:41
0

Create a button programmatically and add it to cell subview.

Aman Aggarwal
  • 3,754
  • 1
  • 19
  • 26
0

You can associate an IBAction with a button programaticly without using nib at all using this code:

[myButton addTarget:self action: @selector(buttonPressed) 
                 forControlEvents:UIControlEventTouchUpInside];
Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • Adding a button, it's not a problem but, add it in my cell causes a crash in my app – izan May 03 '11 at 12:54
0

The crash may be due to following reasons

  1. You have added a view controller to your cell and releasing it. Hence when tapped on button it is not able to find the controller to handle the tap
  2. You have not specified a valid selector when you are adding the button callback for tap event
  3. You may be accessing data which is NULL

If you post some code of how you create your cell, you will be able to get more information on it.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
0

you need to make a method and pass (id )sender to it; try this below ..hope this works..this you write where you crested the button and call method..

[myButton addTarget:self action: @selector(buttonPressed:) 
                 forControlEvents:UIControlEventTouchUpInside];



-(void)buttonPressed:(id)sender{

nslog(@"hi");
}

hope this works

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
vijay adhikari
  • 2,455
  • 1
  • 16
  • 22