Take the 2-minute tour ×

In my app there is a table view. When a row in the table view is selected a UIView appears and shows information. The row title comes from a plist file with strings in it. The plist file also includes strings with a phone number associated with the row title. In the custom UIView I have a button, when you click the button I want it to call the number which is declared in the plist file. The number which is associated with the row the user clicked.

How could I accomplish this?

The action is a ordinary IBAction:

- (IBAction)callNumber:(id)sender;

It is connected to the button in IB (in Xcode 4).

I would appreciate if someone had a solution to my problem, thanks.


For some clarity, I would like to get the phone number which is associated with the row you select in the table view. The string in the plist has a key, for the phone number name: "phone".

NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];

"tablelist" is a NSMutableArray. I would like to get the key "phone" and store it in the NSString *phone. All this in a IBAction. Would it help to post the whole class?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

You need to have a property on the "custom view" that indicates the index path of the row that was selected. You can put this in your custom view controller's header to declare this property:

@interface MyCustomViewController : UIViewController {
    NSIndexPath * indexPath;
@property (nonatomic, retain) NSIndexPath * indexPath;

Then, setup the implementation like this:

@implementation MyCustomViewController
@synthesize indexPath;
// only needed if not using ARC
- (void)dealloc {
    self.indexPath = nil;
    [super dealloc];

Now, in the tableView:didSelectRowAtIndexPath: method, simply create the view controller as usual, but set the indexPath property on it so that it can be accessed in the future:

    MyCustomViewController * vc = [[MyCustomViewController alloc] initWithBundle:nil nibName:nil];
    [vc setIndexPath:indexPath];
    [self presentModalViewController:vc];
    // if not using ARC
    [vc release];

Then, anywhere in MyCustomViewController.m, simply write self.indexPath.row to get the row of the index path that was selected.

share|improve this answer
Thanks for quickly answering. Looks good but I wanted to get the selected row in the same view controller, the "custom view" is a UIView in the same view controller. Then how? Thanks for your help! –  Jacos Nov 23 '11 at 22:37
In that case, simply put the indexPath property on the main view controller, and then put self.indexPath = indexPath in the tableView:didSelectRowAtIndexPath:. Then, use self.indexPath to access the currently selected index path. –  Alex Nichol Nov 24 '11 at 1:59
Thank you so much! Works perfectly now. –  Jacos Nov 24 '11 at 19:58
add comment

There is an elegant solution for this and it is when you setup the UITableViewCell. Set the tag of the UIButton to the index of the row:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* CellIdentifier = @"Cell";

    YourCustomCell* cell = (YourCustomCell*)[tableView 

    //  All the code for loading a cell

    //  Other code you have for configuring the cell

    cell.phoneButton.tag = indexPath.row;

    return cell;    

Then, in the action code you can pull the index out of the tag:

- (IBAction)callNumber:(id)sender {
    UIButton* button = sender;
    int index = button.tag;
    //  Make the phone call
share|improve this answer
This seem promising but I don't think I quite understand the UITableViewCell part. –  Jacos Nov 23 '11 at 22:56
@Jacos Updated. –  Wayne Hartman Nov 23 '11 at 23:45
Nice solution! Thanks –  daidai Jun 14 '12 at 15:10
add comment

What you could do as a quick temporary workaround is the following:

Cell Selected Method: - save an NSUserDefault for what indexPath.row was selected

And in your next view: - read the NSUserDefault value for whichever row was read

This will then let the next view know which one was selected and allow you to get the proper data for it.

share|improve this answer
add comment

If you have controls in a UITableViewCell that perform actions where you need to know the index path there are a few methods you can use:

  1. if you have a single section (or a known section for the cells with controls) and are not using the tag property for something else, store the row + 1 in the tag property. Then when the action is called, retrieve sender.tag - 1 and you've got your row index.

  2. walk up the superview chain from your control until you get to the UITableViewCell. The call [tableView indexPathForCell:cell to get the indexPath.

  3. create a subclass of your control and store the indexPath in the control.

share|improve this answer
add comment

Your Answer


