Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:01:43+00:00 2026-05-29T09:01:43+00:00

I have a UITableViewController (called Details). In that tableView on row 1 I have

  • 0

I have a UITableViewController (called Details). In that tableView on row 1 I have another UITableView which I rotate horizontally. Each cell in that tableview is a custom UITableViewCell (called DetailsHorizontalPhotoCell) which displays a button with an image. I am having a hard time taking action when the photos (the buttons) are pressed.

Calling an IBAction is not a problem for me if I have that IBAction code in the DetailsHorizontalPhotoCell.h/.m files however I need to present a modal VC upon the button presses. That code does not belong and/or work in a tableViewCell – it needs to be in the controller (Details). That being said I can’t figure out how to code it. This is what I have so far.

Details.m:

if( (indexPath.row == 1) && ([detailsObject_.photo count] > 0) )
{
    NSLog(@"** In Photo Section **");

    static NSString *CellIdentifier = @"cellPhoto";
    DetailsHorizontalPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[DetailsHorizontalPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
    cell.horizontalTableView.transform = rotateTable;

    NSArray *photoArray = [[NSArray alloc] initWithArray:detailsObject_.photo];

    cell.horizontalTableView.frame = CGRectMake(0, 0, cell.horizontalTableView.frame.size.width, cell.horizontalTableView.frame.size.height); 
    cell.contentArray = [NSArray arrayWithArray:photoArray];
    cell.horizontalTableView.allowsSelection = YES;

    return cell;
}

DetailsHorizontalPhotoCell.m:

@synthesize horizontalTableView = horizontalTableView_;
@synthesize contentArray = contentArray_;
@synthesize imageButton = imageButton_;

// *** a bunch of code which is not relevant to this question snipped out here... 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    for(UIButton *button in cell.subviews)
    {
        [button removeFromSuperview];
    }

    // create image and button
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
    self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];

    // setup the button
    [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];

    // rotate the button
    CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2);
    imageButton_.transform = rotateButton;

    // this detects the click of each photo and triggers the IBAction
    [imageButton_ addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

    /// do more stuff yada yada yada... <snipped code> and return cell;
}


// the Action (which I know should NOT be in this UITableViewCell class)
- (IBAction)photoButton:(id)sender 
{
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView];
    NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"Image clicked, index: %d", hitIndex.row);

    // presentModalViewController here - but you can't do that from here! Must be in the Details controller
}

So, knowing that I can’t perform a presentModalViewController from the Cell I try to move that code into the Details class (below)

Add the IBAction to Details.h and .m

- (IBAction)photoButton:(id)sender 
{
    DetailsHorizontalPhotoCell *cell = [[DetailsHorizontalPhotoCell alloc] init];

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:cell.horizontalTableView];
    NSIndexPath *hitIndex = [cell.horizontalTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"Image clicked, index: %d", hitIndex.row);
}

And add the click event to the photo button in the Details cellForRowAtIndexPath method.

// Blaaahh! Tried a million combinations of something like below but cannot get it to work... 
//[cell.imageButton addTarget:cell.horizontalTableView action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

PS – I’m using IOS5, Xcode 4.2 w/ ARC

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:01:45+00:00Added an answer on May 29, 2026 at 9:01 am

    First of all there is no need to add the button each time a cell is displayed. You only have to add the subviews of the cell when you create a new cell. You’ll get better scrolling performance this way.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
            // Add image button to cell
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];
            // set tag so you can get the button later
            button.tag = 1021;
            [button addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside]
            // more button config
            [cell.contentView addSubView:button];
        }
        // get image button
        UIButton *button = [cell.contentView viewWithTag:1021];
        // configure image button
        UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
        [button setImage:image forState:UIControlStateNormal];
        return cell;
    }
    

    Next you can use use superview on the button (= sender) to go back to the UITableViewCell. With the cell you can get the indexPath of that cell.

    - (IBAction)photoButton:(id)sender 
    {
        UIView *contentView = [sender superview];
        UITableViewCell *cell = [contentView superview];
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableViewController which has a UIView* called errorView that is used to
I have a UIViewController (called AdjustViewController ) that presents another UIViewController (called SourcePickerViewController )
I have a TabBar App. I have created a UITableView class called Schedule that
I have a UITableViewController which in it's tableView:didSelectRowAtIndexPath method, sets up a view controller,
I have a UITableViewController containing some very basic data. Assume each row just contains
I have a UITableViewController, a bunch of sections and rows, and for each row
I have a UITableView in a UINavigationController that contains a rightBarButtonItem which should hide
I have a UITableViewController. In viewDidLoad I set the rowHeight: self.tableView.rowHeight = 43; But
I have an UITableViewController which I would like to add UIToolbar to with one
I have a subclass of UITableViewController. I have code that can add/remove a UISearchBar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.