0

I am getting the following error when i run my app,

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "2-view-3" nib but didn't get a UITableView.'

The following are the code i am using,

ViewController.h

@interface ViewController : UITableViewController <UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSArray *arForTable;
@property (nonatomic,strong) NSArray *arForSearch;

ViewController.m

@implementation ViewController

@synthesize arForTable = _arForTable;
@synthesize arForSearch = _arForSearch;

-(void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.arForTable = [NSArray arrayWithObjects:
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Pending",@"name",@"2",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Pent",@"name",@"22",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Pen",@"name",@"5",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Neon",@"name",@"7",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Spark",@"name",@"99",@"value",nil],
                       nil];
    self.arForSearch = nil;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (tableView == self.tableView)?self.arForTable.count:self.arForSearch.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

    NSDictionary *dToAccess = (self.tableView==tableView)?[self.arForTable objectAtIndex:indexPath.row] : [self.arForSearch objectAtIndex:indexPath.row];
    [(UILabel*)[cell viewWithTag:1] setText:[dToAccess valueForKey:@"name"]];
    [(UILabel*)[cell viewWithTag:2] setText:[dToAccess valueForKey:@"value"]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)filterContentForSearchText:(NSString*)str{
    // for inCaseSensitive search
    str = [str uppercaseString];

    NSMutableArray *ar=[NSMutableArray array];
    for (NSDictionary *d in self.arForTable) {
        NSString *strOriginal = [d valueForKey:@"name"];
        // for inCaseSensitive search
        strOriginal = [strOriginal uppercaseString];

        if([strOriginal hasPrefix:str]) {
            [ar addObject:d];
        }
    }
    self.arForSearch=[NSArray arrayWithArray:ar];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

Thanks In Advance

shasha
  • 615
  • 2
  • 14
  • 26
  • possible duplicate of [loaded the "rootView" nib but didn't get a UITableView.'](http://stackoverflow.com/questions/4322866/loaded-the-rootview-nib-but-didnt-get-a-uitableview) – Sergey Kalinichenko May 11 '12 at 10:10
  • Your issue is almost certainly the same as in the linked duplicate: replace `UITableViewController` with `UIViewController`, and it will work. – Sergey Kalinichenko May 11 '12 at 10:11
  • 1
    if you are using UITableViewController then there is no need of writing delegate. – Dhara May 11 '12 at 10:13

2 Answers2

1

I don't find any outlets for the UITableView or UISearchBar and moreover you have inherited the UITableViewController and implemented the delegates and datasource which is not required.Also , to have a UISearchBar You will need to create a viewcontroller which will hold the tableview and the uisearchbar as it is not advisable to add a searchbar over tableview and it should never done.So , having a searchbar delegate shows that you have tries to implement a searchbar in tableview .

If you need to add the searchbar in uitableviewcontroller, you can have a button over navigationcontroller. Please, go through this tutorial to check this out.

UISearchBar Tutorial

Sandy
  • 2,572
  • 7
  • 40
  • 61
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
0

Since your class is of UITableViewController type, you don't need to implement UITableViewDataSource and UITableViewDelegate interfaces. Also, you need to have a UITableView object in interface builder and it should be linked as class' view outlet. Then only the screen will be loaded and will run

Hope this helps you in eliminating you issue.

Dipak Mishra
  • 187
  • 12
  • I removed the delegates and also i have the UITableView object in IB and is been linked as class view outlet, even then the issue is there, still i am getting the same error. – shasha May 11 '12 at 10:19
  • Table View delegate and data source method are just for using multiple tables. In your case, you are using only one table i.e self.tableView. Also you are not creating any cell but asking table to dequeue cells for you and setting it properties. just debug that you have a cell or not. – fibnochi May 11 '12 at 11:03