Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I get the indexPath of UIButton in a customized tableViewCell?

I created a tableViewCell , labels and a uibutton, when i want to know the indexPath it appears null.

@interface AgrupacionesViewCell : UITableViewCell

@property (nonatomic, retain) IBOutlet UILabel *lblagrupacion;

@property (nonatomic, retain) IBOutlet UILabel *lblingresos;

@property (nonatomic, retain) IBOutlet UILabel *lblcuartos;

@property (nonatomic, retain) IBOutlet UILabel *lbltarifa;

@property (nonatomic,strong) IBOutlet UIButton *btnVerHoteles;

@end

//--- AgrupacionesViewController.h --------

import <UIKit/UIKit.h>

import <CoreData/CoreData.h>

@interface AgrupacionesViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate, NSXMLParserDelegate> {

IBOutlet UITableView *tableviewagrupaciones;

IBOutlet UIActivityIndicatorView *IndicatorViewClientes;

NSMutableArray *array;
//---web service access---
NSMutableData *webData;
NSMutableString *soapResults;
NSURLConnection *conn;
//---xml parsing---
NSXMLParser *xmlParser;
BOOL elementFound;
IBOutlet UILabel *titulohotel;

}

@property (nonatomic, retain) UILabel *titulohotel;

@property(nonatomic, retain) UITableView *tableviewagrupaciones;

@property(nonatomic, retain) UIActivityIndicatorView *IndicatorViewClientes;

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

-(IBAction)returnpage;

@end

//--- AgrupacionesViewController.m------

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    HotelesAgrupacionViewController *vcDestino = [segue destinationViewController];
    
    NSIndexPath *indexPath = [self.tableviewagrupaciones indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableviewagrupaciones cellForRowAtIndexPath:indexPath];
    NSString *Agrupacion = [[NSString alloc]initWithString:cell.textLabel.text];
    
    vcDestino.strAgrupacion = Agrupacion;
    

}

1 Answer

When you create the cell , you have to set there the tag like this: cell.btn.tag = indexPath.row; Then, set the the Target to that button: [cell.btn addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];

And finally, in your method:

-(void)method:(id)sender { //Catch the button UIButton *index = (UIButton *)sender; //And then get the tag... int tag = index.tag; }