运行我的应用程序时出现以下错误,
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UITableViewController loadView] 加载了“2-view-3”笔尖,但没有获得 UITableView。
以下是我正在使用的代码,
视图控制器.h
@interface ViewController : UITableViewController <UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSArray *arForTable;
@property (nonatomic,strong) NSArray *arForSearch;
视图控制器.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);
}
提前致谢