Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a button in UITableviewCell [closed]

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

like image 960
izan Avatar asked Apr 30 '26 19:04

izan


2 Answers

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.

like image 88
Tim Avatar answered May 03 '26 09:05

Tim


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 
    }
like image 25
Sandeep Avatar answered May 03 '26 10:05

Sandeep