Cargó la punta pero no obtuvo una UITableView

I have been following this tutorial on YouTube (en la parte 1 y en la parte 2).

I have completed both videos and have hooked up the view controller with the parent view controller using this code:

- (IBAction)searchButtonClicked:(id)sender {
    NSLog(@"It works.");

    SearchViewController *searchViewControl = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchControllerNav"];

    [self presentViewController:searchViewControl animated:YES completion:nil];

}

This code indeed works since this is the same format that I use for my other modal view controllers, so i know that's not the problem.

Anyway, when I tap on the search button in the view controller, it should pop up the SearchViewController. However, the app crashes instead and it gives me this error message:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "jp7-vt-IdA-view-Jer-xW-qlD" nib but didn't get a UITableView.'

I am using Storyboards for this app.

Is there something that I'm missing? Thank you in advance.

A side question: I'm also getting a warning, saying Comparison between pointer and integer ('BOOL *' (aka 'signed char *') and 'int') cuando isFiltered == YES is shown. Is there anyway to fix it?

Aquí está el código para SearchViewController:

SearchController.h

#import <UIKit/UIKit.h>

@interface SearchViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {

}
- (IBAction)cancelButtonTapped:(id)sender;

@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic, strong) NSMutableArray *itemsInCloudApp;
@property (nonatomic, strong) NSMutableArray *filteredList;
@property BOOL *isFiltered;


@end

SearchViewController.m

#import "SearchViewController.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

@synthesize mySearchBar, myTableView, itemsInCloudApp, filteredList, isFiltered;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set title.
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.text = @"Search";
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.clipsToBounds = YES;
    titleLabel.numberOfLines = 1;
    titleLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:18];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel sizeToFit];

    self.navigationItem.titleView = titleLabel;

    // Alloc and init list.
    itemsInCloudApp = [[NSMutableArray alloc]initWithObjects:@"http://www.apple.com/", @"http://www.trijstudios.com/", @"http://www.google.com/", @"http://www.squarespace.com/", @"http://www.youtube.com/", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (isFiltered == YES) {
        return [filteredList count];
    } else {
    return [itemsInCloudApp count];
    }
}

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

    // Configure the cell...

    if (isFiltered == YES) {
        cell.textLabel.text = [filteredList objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [filteredList objectAtIndex:indexPath.row];;
    } else {
        cell.textLabel.text = [itemsInCloudApp objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [itemsInCloudApp objectAtIndex:indexPath.row];
    }

    return cell;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchText.length == 0) {
        // Set bollean flag
        isFiltered = NO;
    } else {
        // Set boolean flag
        isFiltered = YES;

        // Alloc and init our fliteredData
        filteredList = [[NSMutableArray alloc] init];

        // Fast enumeration
        for (NSString *name in itemsInCloudApp) {
            NSRange nameRange = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (nameRange.location != NSNotFound) {
                [filteredList addObject:name];
            }
        }
    }
    // Reload tableView
    [myTableView reloadData];

}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [mySearchBar resignFirstResponder];
}

- (IBAction)cancelButtonTapped:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}
@end

NOTE: There are a few edits that I made to fit my needs.

preguntado el 27 de noviembre de 13 a las 05:11

@Dinesh That was actually the first one that I went to before asking this question, but it didn't help. -

I posted an answer over here stackoverflow.com/questions/11221802/… I hope it helps you and please vote if it does. Thanks! -

5 Respuestas

have you tried changing your @interface SearchViewController : UITableViewController a @interface SearchViewController : UIViewController

I strongly suspect that either you have not attached your UITableview as View in XIB or your class should be derived UIViewController instead of UITableviewController class..

respondido 27 nov., 13:06

Oddly enough, your suggestion worked. I'm not too sure why it doesn't take UITableViewController even though it clearly has a UITableView, but it works. Thank you for your help. - crisjr

I had a similar error. I was able to resolve it using @Dinesh's suggestion, but I didn't like this because I was afraid that there could be some unintended consequences.

What I figured out was that when I looked at the scene hierarchy in the storyboard, I noticed that I had this structure (sorry, I don't know how to format this - it's supposed to be a tree structure):

View Controller
  View
     Table View

When I took out the View that sat in the middle, my problem went away. However, before doing so, you need to delete any outlets that might exist between the view and either the view controller or the table view. After you make sure that these are gone, follow these final steps:

  1. Drag the Table View so that it is a direct descendant of the View Controller.
  2. Eliminar la vista
  3. Command-Drag from the View Controller to the Table View, thereby creating a new outlet directly between the two.

Also, leave the .h file as a subclass of UITableView (¿No estás registrado como UIView).

Anyway, that solved the issue for me. If anyone comes across this, I hope that it helps.

respondido 08 nov., 16:23

Oddly enough, it solved my problem too. Seems like a horrible misunderstanding between internal frameworks! Thanks for the tip it was very insightful. - M. Porooshani

For you side question regrading the warning, the warning comes because you have made the BOOL isFiltered as a pointer.

respondido 27 nov., 13:05

For your first problem, you needed to check the storyboard. I am sure that your file owner's view está conectado a un Vista de interfaz de usuario. To solve this, you must drag UITableView and view must be connected to the UITableView.

For your second problem, declare BOOL as

@property(assign,nonatomic) BOOL isFiltered;

respondido 27 nov., 13:06

I actually don't have a xib file. I should have mentioned that I am using storyboards. - crisjr

@Junior117 for storyboard, use same method. check your viewcontroller's connection panel - manujmv

I encountered this when building a iOS7 Universal app with a simple, dumb error: I'd built part of the iPhone app only, but had the scheme set to iPad simulator. After getting the error and looking here, I saw my mistake, switched the scheme to iPhone, and the app ran with the proper storyboard for the proper simulator. Hope that helps.

Respondido el 23 de enero de 14 a las 17:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.