我收到以下錯誤,當我運行我的應用程序,的UISearchBar和UITableView的
終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,原因是:「 - [UITableViewController中的loadView]加載的‘2 - 視圖 - 3’筆尖但沒有得到一個UITableView。「
以下是我使用的代碼,
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);
}
在此先感謝
的可能重複[加載 「rootView」 筆尖,但沒有得到一個UITableView。「(http://stackoverflow.com/questions/4322866/loaded-the-rootview-nib-but -didnt-get-a-uitableview) – dasblinkenlight
你的問題幾乎可以肯定的與鏈接副本相同:用'UIViewController'替換'UITableViewController',它就可以工作。 – dasblinkenlight
如果您使用UITableViewController,則不需要編寫委託。 – Dhara