Ashish Agarwal
Ashish Agarwal

Reputation: 14925

tableView not found on object of type

I have a class named FirstViewController which is a subclass of UIViewController and I have a UITableView in the class. I need to transition from this class to a new UIViewController subclass (using segues) when a row is pressed.

I have made the FirstViewController the UITableViewDelegate and UITableViewDataSource using the code -

@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

I am getting an error on this line of code -

NSIndexPath *path = [self.tableView indexPathForSelectedRow];

How do I fix this problem since the tableView is found in UITableViewController and not in UIViewController class ?

Edit -

Here's the code related to my table view -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [sarray count];
}

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

    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }

    NSString *text = [sarray objectAtIndex:[indexPath row]];
    cell.textLabel.text = text;
    return cell;
}

Upvotes: 1

Views: 4738

Answers (3)

Lineesh K Mohan
Lineesh K Mohan

Reputation: 1712

you can simply declare a table view property on FirstViewController using:

@property(nonatomic, strong) IBOutlet UITableView *tableView;

you then need to connect this property to the Table View in the story board.

Upvotes: 1

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

You can simply declare tableView as a property:

@property(nonatomic, strong) IBOutlet UITableView *tableView;

Upvotes: 5

thelaws
thelaws

Reputation: 8001

You get a free UITableView only if you subclass UITableViewController,

@interface FirstViewController : UITableViewController

Upvotes: 4

Related Questions