Uitableview 故事板IOS6中的Uisearchbar和Searchcontroller

Uitableview 故事板IOS6中的Uisearchbar和Searchcontroller,uitableview,uiviewcontroller,uisearchbar,uisearchdisplaycontroller,Uitableview,Uiviewcontroller,Uisearchbar,Uisearchdisplaycontroller,我有下面的搜索实用性很好,但是我想把这段代码添加到另一个使用故事板的项目中。UIViewcontroller工作正常,甚至通过JSON从数据库返回数据,但我一直收到这个错误 *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView数据源必须从tableView返回单元格:CellForRowatineXpath:” 我在故事板上添加了导航控制器和Uiviewcontroller,还添加了表格和自定义单元格 我还在UIvi

我有下面的搜索实用性很好,但是我想把这段代码添加到另一个使用故事板的项目中。UIViewcontroller工作正常,甚至通过JSON从数据库返回数据,但我一直收到这个错误

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView数据源必须从tableView返回单元格:CellForRowatineXpath:”

我在故事板上添加了导航控制器和Uiviewcontroller,还添加了表格和自定义单元格

我还在UIviewcontroller中制作了searchbar和searchbar控制器输出,并将它们拖动到情节提要上并链接起来

我不知道为什么在uisearchbar上执行搜索时,会立即抛出上面的错误

searchbarcontroller从uitableview接收其数据源

这是代码的一部分

`#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
导入 @接口MyController:UIViewController{ UITableView*myTable; 尤尼布*迈塞尔; //保存json数据的变量 NSMutableArray*ids; NSMutableArray*名称; NSMutableArray*价格; NSMutableArray*位置; NSMutableArray*拇指; NSMutableArray*说明; //搜索栏的东西 ibuisearchbar*mySearchBar; IBOUTLE UISearchDisplayController*搜索控制器; //如果内存警告删除了视图,则搜索UI的保存状态。 NSString*savedSearchTerm; NSInteger保存的ScopeButtonIndex; NSMutableArray*searchedID; NSMutableArray*searchedName; NSMutableArray*searchedPrice; NSMutableArray*搜索位置; NSMUTABLEARRY*searchedThumbs; NSMutableArray*说明; } @属性(非原子,保留)IBUITableView*myTable; @属性(非原子,保留)IBMOutlet UISearchDisplayController*searchController; @属性(非原子,保留)ibuisearchbar*mySearchBar; @属性(非原子,保留)NSMutableArray*id; @属性(非原子,保留)NSMutableArray*名称; @属性(非原子,保留)NSMutableArray*价格; @属性(非原子,保留)NSMutableArray*位置; @属性(非原子,保留)NSMutableArray*thumb; @属性(非原子,保留)NSMutableArray*说明; @属性(非原子,复制)NSString*savedSearchTerm; @属性(非原子)NSInteger保存的ScopeButtonIndex; @属性(非原子)布尔搜索是活动的; @属性(非原子,保留)NSMutableArray*searchedID; @属性(非原子,保留)NSMutableArray*searchedName; @属性(非原子,保留)NSMutableArray*searchedPrice; @属性(非原子,保留)NSMutableArray*searchedLocation; @属性(非原子,保留)NSMutableArray*searchedThumbs; @属性(非原子,保留)NSMutableArray*searchedDescription; -(void)重新加载TableViewDataSource; -(void)doneLoadingTableViewData; @结束 MyController.m文件 #导入“MyController.h” @myController的实现 @合成myTable; @综合数据,搜索数据 @合成savedSearchTerm、savedScopeButtonIndex、searchWasActive; //添加搜索栏 #布拉格标记- #pragma标记表视图数据源 -(NSInteger)表格视图中的节数:(UITableView*)表格视图{ //返回节数。 返回1; } -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{ //返回节中的行数。 if(tableView==self.searchDisplayController.searchResultsTableView){ 返回[self.searchedds计数]; }否则{ 返回[self.ids count]; } } //自定义表格视图单元格的外观。 -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ MyCustomCell*单元格=(MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:@“MyCellID]”; 如果(!单元格) { NSArray*topLevelItems=[myCell实例化所有者:自选项:nil]; 单元格=[topLevelItems对象索引: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]]; //使用异步技术设置映像 [[cell thumbs]setImageWithURL:[NSURL URLWithString:[SearchedTumbs objectAtIndex:indexPath.row]]占位符图像:[UIImage ImageName:@“placeholder.png”]; }否则{ [[cell titles]setText:[title objectAtIndex:indexath.row]]; [[cell prices]setText:[NSString stringWithFormat:@“%@,[price objectAtIndex:indexPath.row]]; [[cell locations]setText:[NSString stringWithFormat:@“%@,[location objectAtIndex:indexPath.row]]; //使用异步技术设置映像 [[cell thumbs]setImageWithURL:[NSURL URLWithString:[thumb objectAtIndex:indexPath.row]]占位符图像:[UIImage ImageName:@“placeholder.png”]; } 返回单元; } ` [1]: http://i.stack.imgur.com/0ZNzq.png 尝试这种方法(将self添加到tableView):

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