2012-05-11 57 views
0

我收到以下錯誤,當我運行我的應用程序,的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); 
} 

在此先感謝

+0

的可能重複[加載 「rootView」 筆尖,但沒有得到一個UITableView。「(http://stackoverflow.com/questions/4322866/loaded-the-rootview-nib-but -didnt-get-a-uitableview) – dasblinkenlight

+0

你的問題幾乎可以肯定的與鏈接副本相同:用'UIViewController'替換'UITableViewController',它就可以工作。 – dasblinkenlight

+1

如果您使用UITableViewController,則不需要編寫委託。 – Dhara

回答

1

我不覺得對的UITableView或任何的UISearchBar網點,而且你已經繼承了的UITableViewController並實現了代表和數據源這是不required.Also,有一個UISearchBar您將需要創建一個視圖 - 控制將舉行tableview和uisearchbar,因爲它不建議在tableview上添加一個搜索欄,它應該永遠不會完成。因此,有一個搜索欄委託顯示你已經嘗試在tableview中實現一個搜索欄。

如果您需要在uitableviewcontroller中添加搜索欄,您可以在navigationcontroller上添加一個按鈕。請通過本教程來檢查這一點。

UISearchBar Tutorial

0

由於類是的UITableViewController的類型,你不需要實現UITableViewDataSource和UITableViewDeleg吃了接口。另外,你需要在界面構建器中有一個UITableView對象,並且它應該鏈接爲類的視圖插座。然後只有屏幕將被加載,並將運行

希望這可以幫助你消除你的問題。

+0

我刪除了代表,並且我有IB中的UITableView對象,並且已經作爲類視圖插座進行了鏈接,即使那樣,問題仍然存在,但我仍然得到相同的錯誤。 – shasha

+0

表視圖委託和數據源方法僅用於使用多個表。在你的情況下,你只使用一個表,即self.tableView。此外,您並未創建任何單元格,而是要求表格爲您取出單元格並設置屬性。只是調試你有沒有一個單元格。 – fibnochi

相關問題