TerryTough
TerryTough

Reputation: 51

Iphone UITableView custom cell button event

I have a UITableView which has custom cells as its rows. In these custom cells they have 2 buttons.

I catch the click of the button in the custom cell class. Just wondering how i pass that event from the custom cell class through to the parent view controller that is holding the UITableView control. Advice?

Thanks

Upvotes: 4

Views: 5536

Answers (2)

chilitechno.com
chilitechno.com

Reputation: 1575

You can do this:

- (void)buttonTapped:(id)sender event:(id)event
{
    CGPoint touchPosition = [[[event allTouches] anyObject] locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPosition];
    if (indexPath != nil)
    {
         //do Something here
    }
}

// in cellForForAtIndexPath, bind the selector:

UIButton *button = (UIButton *)[cell viewWithTag:1];
[button addTarget:self action:@selector(buttonTapped:event:) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 4

mvds
mvds

Reputation: 47114

You can specify the actions a UIButton performs using

[button addTarget:self action:@selector(doStuff:)
             forControlEvents:UIControlEventTouchDown];

review the UIControl documentation for more information. (UIButton inherits from UIControl)

Upvotes: 2

Related Questions