I'm trying to figure this out for some time now. I am creating a UIButton programmatically within a tableview cell. Upon clicking the button I want to segue or push to another view controller. This is a piece of cake if I use StoryBoard, create the button there, and then control-drag a segue to the destination VC. I however do not want to do it this way for various reasons. Below is my code thus far.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// <<<< bunch of code snipped out here to keep this example short >>>
for(UIButton *button in cell.subviews)
{
[button removeFromSuperview];
}
UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];
[imageButton setImage:image forState:UIControlStateNormal];
imageButton.layer.masksToBounds = YES;
imageButton.layer.cornerRadius = 5.0;
imageButton.layer.borderWidth = 1.0;
imageButton.layer.borderColor = [[UIColor grayColor] CGColor];
CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2);
imageButton.transform = rotateButton;
cell.backgroundColor = [UIColor clearColor];
[cell addSubview:imageButton];
return cell;
Now, for each row in this table a button with an image will be created programmatically. I also rotate the button - yes this is a horizontal table view.
So how do I take action on button clicks? All buttons would go to the same destination view controller...
PS - I'm using Xcode 4.2 with ARC (not sure that really matters).
I've implemented the code suggestion (Thanks jrturton) however I believe that because I'm in a UITableView cell I cannot present a modal view controller. I now have the following code...
.h file:
@interface DetailsHorizontalPhotoCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *horizontalTableView;
@property (nonatomic, strong) NSArray *contentArray;
- (IBAction)photoButton:(id)sender;
@end
.m file:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/// ----- code here to setup the cell, button, image, etc...
// do something when the buttong is pressed
[imageButton addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)photoButton:(id)sender
{
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView];
NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint];
// do some stuff here to pull object data out of the contentArray
NSLog(@"Image clicked, index: %d", hitIndex.row);
// other non-relevant code snipped to keep this example short
// next I want to present a modal view controller (lets call it PhotoAlbum) which is a TableViewController
PhotoAlbum *photoAlbum = [[PhotoAlbum alloc] init];
// below does not work, does not even autocomplete in Xcode. I believe its because I'm in a UITableViewCell and not a VC... Any suggestions here?
[photoAlbum setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:photoAlbum animated:YES];
}
I'm not sure why you are recreating the button each time in the cell instead of changing the image, but let's ignore that for now.
You can connect all your buttons to the same action in your table view controller, this action can take a sender
argument as usual.
You add an action like so:
[imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
This calls a method in your view controller with the signature:
-(void)buttonPressed:(UIButton * sender)
In this method, you can detect which row was clicked as follows:
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
And then perform whatever other actions you like.