Question

I have a view controller with a button. When the user clicks the button, I want the user to be navigated to a table view controller (i.e. be shown a table view).

When button is clicked, i have it call the following method:

- (void)loadTableViewController{

    TableViewViewController *tableViewController = [[TableViewController alloc]     initWithNibName:nil bundle:NULL];
    [self.navigationController pushViewController:tableViewController animated:YES];

}

The above code seems to be fine since in debug mode after the last line, I am taken to the TableView Controller's implementation file. Here is where things break down... I declared tableView as a property of the tableViewController

Here is the code for the viewDidLoad method:

[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:self.tableView];

The program breaks down after the last line in Debug mode. Not sure at all what is wrong... the only other changes I have made are to return 1 section and 1 row for the table. Here is that code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = @"Test";
    return cell;
}

EDIT: adding error details.

The error I get is looks like a generic "libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) "

Please keep in mind that I am very new to Xcode and programming in general. So I may not be looking at the right place to find the specific error.

Was it helpful?

Solution

You are not using UITableViewController's initialisation method. Instead, use

TableViewViewController *tableViewController = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableViewController animated:YES];

After that, initialising your table view inside your UITableViewController class is pointless, because UITableViewController is already doing that for you (that's the point of using it) and instead, access the tableView property with self.tableView.

Also note that - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier is only available for iOS 6.0 and above (will crash below that)

And for populating the cell just use :

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }


    cell.textLabel.text = [NSString stringWithFormat:@"row %d", indexPath.row + 1];
    return cell;
}

OTHER TIPS

My guess would be the problem is caused by this line:

TableViewViewController *tableViewController = [[TableViewController alloc]     initWithNibName:nil bundle:NULL];

You are missing the actual nib name. Replace nil with your nib name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top