views:

176

answers:

1

Hi,

I have a table view controller with custom cells. In those cells i added a button for each one of the cells. What i would like it's that when I press that button it display a new view with more information about that cell, different of the view that i get from didSelectRowAtIndexPath.

I know it's now a hard question. I've seen that the button has some action on interfacebuilder (touch down maybe) but how do i link it to what code. where should i declare the code that handle that event?

I've posted in other forums with no answer, hope this would work.

Thanks. Gian

A: 

I would probably subclass UIButton to have an instance of NSIndexPath. That way, each individual UIButton in a UITableViewCell can "know" where it is in the table view, so when you press the button, you can call some method that takes an NSIndexPath and pushes a new view, similar to what you do in -didSelectRowAtIndexPath: but with your other view instead (maybe give it a descriptive method name like -didPressButtonAtIndexPath:).

Instead of using the Interface Builder to do this, you should add a method to the UIButton subclass itself that in turn calls a method on your view controller. Then, for each UIButton, you can use the UIControl method -addTarget:action:forControlEvents:. Have the UIButton call its own method, which calls the controller's method. Your solution might look something like:

// MyButton.h
@interface MyButton : UIButton {
    NSIndexPath *myIndexPath;
    MyViewController *viewController;
}

- (void)didPressButton;
@end

// MyViewController.h
@interface MyViewController { }
- (void)didPressButtonAtIndexPath:(NSIndexPath *)indexPath;
@end

Then, when you build your cells, for each button you add call:

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

And finally, implement -didPressButton to look like:

- (void)didPressButton {
   [controller didPressButtonAtIndexPath:myIndexPath];
}
Tim
Thanks Tim. I was just about to answer my own question. I took another approach.Before this question I had to customize a cell so I made a subclass of UITableViewCell to add images and extra labels. So, also, in IB I added a button. In my code, the subclass cell, I added a IBACtion and connect that action with the "Touch Down" event of that button. For know i printed something in NSLog to know I was gettin the event. For what i see, right now i can't know which button was pressed. I guess your solution solves this. right? The problem is that i don't understand much, @selector for example
gvalero87
BTW thanks for you reply it is really good. It just I'm really new at this. Just one week programming in all related to mac :S... Right now i'm trying to figure out how to get a new modal view when you press that button.
gvalero87
Hi Tim, please answer some questions I have. You said that when I build my cell i should do (code 2). I have a tableviewcell subclass that is connected to xib file which holds the view for that cell, so I just add a button to that view and connected it to a IBOutle in the tableViewcell subclass. So, What do you mean when creating this Cell?
gvalero87
@gvalero87 ah - I didn't know you were using IB for your cell views. I tend to create my cells programmatically in the table view data source's class. If you have the button connected to an IBOutlet for your cell view already, then all you have to do is create an IBAction in the cell view's code, then connect the button's touch down event to that IBAction. That should take the place of (code 2), and the body of your IBAction will be (code 3) with `IBAction` replacing `void`.
Tim