Hoppo
Hoppo

Reputation: 1170

self.tableView insertRowsAtIndexPaths from within tableView delegate

So I thought I'd have a go at building my own simple app. Please go easy on me I'm new to all this! The idea is this. For iPad have a single view controller with a text box and a text field. Text box takes a title, and text field takes the body of a report. There's a button on the page to submit the report, which bundles the two texts into an object and adds it to a table view within the same view controller. I have set the view controller as a delegate with <UITableViewDelegate, UITableViewDataSource> in my header file. My table view works fine for adding items in the viewDidLoad method. But adding items from the text inputs via a UIButton connected to -(IBAction) addItem falls over with: Property 'tableView' not found on object of type 'ReportsViewController'

- (IBAction)addReportItem
{
int newRowIndex = [reports count];

ReportObject *item = [[ReportObject alloc] init];
item.title = @"A new title";
item.reportText = @"A new text";
[reports addObject:item];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths   withRowAnimation:UITableViewRowAnimationAutomatic];
}

I understand that I'm trying to call a method within my object but I have other method calls to tableView which work fine. i.e.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [reports count];
}

I thought this was the point of delegation. I know I'm missing something, but as I say I am new to all this and have looked everywhere for an answer before posting. What do I need to do to send my IBAction message to tableView?

Upvotes: 1

Views: 1395

Answers (2)

gerdmuller.de
gerdmuller.de

Reputation: 33

I had the same problem. What helped was to inherit the View Controller from UITableViewController, instead of UIViewController. Not using the protocol names in angled brackets. The TableView is then linked to the dataSource and delegate via the storyboard (resp. InterfaceBuilder).

The parent class UITableViewController has an IBOutlet tableView defined.

MyViewController.h:

@interface MyViewController : UITableViewController

Upvotes: 1

timthetoolman
timthetoolman

Reputation: 4623

Do you have a tableView instance variable setup in your .h file of the view controller?

The reason you are able to access it in the delegate and data source methods is because they are passed in as part if the methods.

You will need to add the IBOUTLET tableView ivar and connect it to the tableView in your .xib.

Or perhaps your ivar for the tableView is named something else?

Good luck.

Upvotes: 1

Related Questions