0
votes

I have the following search working fine pragmatically, however I would like to add this code to another project that uses storyboard. the UIViewcontroller works ok and even returns data from the database through JSON, however I keep getting this error

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

I have added navigation controller plus Uiviewcontroller on the storyboard, added table and custom cell as well

I have also made searchbar and searchbar-controllers outlets in the UIviewcontroller and dragged and linked them up on the storyboard

I don't know why when I perform a search on uisearchbar, immediately the error above is thrown

searchbarcontroller receives its data-source from uitableview

Here is part of the code

`#import <UIKit/UIKit.h>

@interface MyController : UIViewController < UITableViewDelegate, UITableViewDataSource> {
    UITableView *myTable;
    UINib *myCell;

    //Vars to hold json data
    NSMutableArray *ids;
    NSMutableArray *name;
    NSMutableArray *price;
    NSMutableArray *place;
    NSMutableArray *thumb;
    NSMutableArray *description;

    //Searchbar stuff
    IBOutlet UISearchBar *mySearchBar;
    IBOutlet UISearchDisplayController *searchController;

    // The saved state of the search UI if a memory warning removed the view.
    NSString    *savedSearchTerm;
    NSInteger   savedScopeButtonIndex;
    NSMutableArray *searchedID;
    NSMutableArray *searchedName;
    NSMutableArray *searchedPrice;
    NSMutableArray *searchedLocation;
    NSMutableArray *searchedThumbs;
    NSMutableArray *description;


}


   @property (nonatomic, retain) IBOutlet UITableView *myTable;
    @property (nonatomic, retain) IBOutlet UISearchDisplayController *searchController;
    @property (nonatomic, retain) IBOutlet UISearchBar *mySearchBar;

    @property (nonatomic, retain) NSMutableArray *ids;
    @property (nonatomic, retain) NSMutableArray *name;
    @property (nonatomic, retain) NSMutableArray *price;
    @property (nonatomic, retain) NSMutableArray *place;
    @property (nonatomic, retain) NSMutableArray *thumb;
    @property (nonatomic, retain) NSMutableArray *description;

    @property (nonatomic, copy) NSString *savedSearchTerm;
    @property (nonatomic) NSInteger savedScopeButtonIndex;
    @property (nonatomic) BOOL searchWasActive;

    @property (nonatomic, retain) NSMutableArray *searchedID;
    @property (nonatomic, retain) NSMutableArray *searchedName;
    @property (nonatomic, retain) NSMutableArray *searchedPrice;
    @property (nonatomic, retain) NSMutableArray *searchedLocation;
    @property (nonatomic, retain) NSMutableArray *searchedThumbs;
    @property (nonatomic, retain) NSMutableArray *searchedDescription;

    - (void)reloadTableViewDataSource;
    - (void)doneLoadingTableViewData;
    @end





MyController.m file



#import "MyController.h"

@implementation myController
@synthesize myTable;
@synthesize data, searcheddata
@synthesize savedSearchTerm, savedScopeButtonIndex, searchWasActive;

//Add the search bar

#pragma mark -
#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 (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchedIds count];
    } else {
        return [self.ids count];
    }
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCellID"];


    if (!cell)
    {
        NSArray *topLevelItems = [myCell instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [[cell titles] setText:[searchedTitles objectAtIndex:indexPath.row]];
        [[cell prices] setText:[NSString stringWithFormat:@"%@", [searchedPrices objectAtIndex:indexPath.row]]];
        [[cell locations] setText:[NSString stringWithFormat:@"%@", [searchedLocation objectAtIndex:indexPath.row]]];
        //Set the image with async technology
        [[cell thumbs] setImageWithURL:[NSURL URLWithString:[searchedThumbs objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


    } else {
        [[cell titles] setText:[title objectAtIndex:indexPath.row]];
        [[cell prices] setText:[NSString stringWithFormat:@"%@", [price objectAtIndex:indexPath.row]]];
        [[cell locations] setText:[NSString stringWithFormat:@"%@", [location objectAtIndex:indexPath.row]]];
        //Set the image with async technology
        [[cell thumbs] setImageWithURL:[NSURL URLWithString:[thumb objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    }
    return cell;
}
`


  [1]: http://i.stack.imgur.com/0ZNzq.png
1

1 Answers

0
votes

Try this approach (add self to tableView):

MyCustomCell *cell = (MyCustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MyCellID"];