开发者

a button in UITableviewCell [closed]

开发者 https://www.devze.com 2023-03-02 03:20 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question 开发者_如何学Go

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


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.


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 
    }


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;
}


Create a button programmatically and add it to cell subview.


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];


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消