我有以下搜索工作正常,但我想将此代码添加到另一个使用storyboard的项目。 UIViewcontroller工作正常,甚至通过JSON从数据库返回数据,但是我不断收到此错误
* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:'
我在故事板上添加了导航控制器和Uiviewcontroller,还添加了表格和自定义单元格
我还在UIviewcontroller中创建了搜索栏和搜索栏控制器插座,并在故事板上将它们拖放并链接起来
我不知道为什么当我在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
答案 0 :(得分:0)
尝试这种方法(将self添加到tableView):
MyCustomCell *cell = (MyCustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MyCellID"];