2013-11-27 66 views
3

我一直在YouTube上關注本教程(part 1part 2)。加載筆尖,但沒有得到UITableView

我已經完成了兩部影片,並迷上了使用此代碼父視圖控制器視圖控制器:

- (IBAction)searchButtonClicked:(id)sender { 
    NSLog(@"It works."); 

    SearchViewController *searchViewControl = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchControllerNav"]; 

    [self presentViewController:searchViewControl animated:YES completion:nil]; 

} 

此代碼確實是工作,因爲這是我用我的其他模式的看法相同的格式控制器,所以我知道這不是問題。

無論如何,當我點擊視圖控制器中的搜索按鈕時,它應該彈出SearchViewController。然而,該應用程序崩潰,它給了我這個錯誤消息:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "jp7-vt-IdA-view-Jer-xW-qlD" nib but didn't get a UITableView.' 

我正在使用此應用程序的故事板。

有什麼,我失蹤?先謝謝你。

一個側面的問題:我也收到一個警告,說Comparison between pointer and integer ('BOOL *' (aka 'signed char *') and 'int')每當isFiltered == YES顯示。無論如何解決它?

這裏是SearchViewController代碼:

SearchController.h

#import <UIKit/UIKit.h> 

@interface SearchViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> { 

} 
- (IBAction)cancelButtonTapped:(id)sender; 

@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar; 
@property (weak, nonatomic) IBOutlet UITableView *myTableView; 

@property (nonatomic, strong) NSMutableArray *itemsInCloudApp; 
@property (nonatomic, strong) NSMutableArray *filteredList; 
@property BOOL *isFiltered; 


@end 

SearchViewController.m

#import "SearchViewController.h" 

@interface SearchViewController() 

@end 

@implementation SearchViewController 

@synthesize mySearchBar, myTableView, itemsInCloudApp, filteredList, isFiltered; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Set title. 
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    titleLabel.text = @"Search"; 
    titleLabel.adjustsFontSizeToFitWidth = YES; 
    titleLabel.clipsToBounds = YES; 
    titleLabel.numberOfLines = 1; 
    titleLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:18]; 
    titleLabel.textColor = [UIColor blackColor]; 
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
    titleLabel.textAlignment = NSTextAlignmentCenter; 
    [titleLabel sizeToFit]; 

    self.navigationItem.titleView = titleLabel; 

    // Alloc and init list. 
    itemsInCloudApp = [[NSMutableArray alloc]initWithObjects:@"http://www.apple.com/", @"http://www.trijstudios.com/", @"http://www.google.com/", @"http://www.squarespace.com/", @"http://www.youtube.com/", nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#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 (isFiltered == YES) { 
     return [filteredList count]; 
    } else { 
    return [itemsInCloudApp count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 

    if (isFiltered == YES) { 
     cell.textLabel.text = [filteredList objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [filteredList objectAtIndex:indexPath.row];; 
    } else { 
     cell.textLabel.text = [itemsInCloudApp objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [itemsInCloudApp objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    if (searchText.length == 0) { 
     // Set bollean flag 
     isFiltered = NO; 
    } else { 
     // Set boolean flag 
     isFiltered = YES; 

     // Alloc and init our fliteredData 
     filteredList = [[NSMutableArray alloc] init]; 

     // Fast enumeration 
     for (NSString *name in itemsInCloudApp) { 
      NSRange nameRange = [name rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

      if (nameRange.location != NSNotFound) { 
       [filteredList addObject:name]; 
      } 
     } 
    } 
    // Reload tableView 
    [myTableView reloadData]; 

} 

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [mySearchBar resignFirstResponder]; 
} 

- (IBAction)cancelButtonTapped:(id)sender { 

    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
@end 

注:有,我給做了一些修改符合我的需求。

+0

檢查此.. http://stackoverflow.com/questions/11221802/nib-but-didnt-get-a-uitableview – Dinesh

+0

@Dinesh這實際上是我問前提出的第一個問題,但它沒有幫助。 – chrisjr

+0

我張貼在這裏http://stackoverflow.com/questions/11221802/nib-but-didnt-get-a-uitableview我希望答案可以幫助你,請投票,如果它。謝謝! –

回答

19

你試圖改變你的@interface SearchViewController : UITableViewController@interface SearchViewController : UIViewController

我強烈懷疑,要麼你不附加您的UITableView的視圖中XIB或你類應該派生UIViewController而不是UITableviewController類。

+1

奇怪的是,你的建議工作。我不太確定它爲什麼不顯示'UITableViewController',即使它顯然具有'UITableView',但它可以工作。感謝您的幫助。 – chrisjr

2

對於你的問題重新警告,警告來自因爲你已經使BOOL被過濾爲一個指針。

2

對於第一個問題,您需要檢查故事板。我確信您的文件擁有者的查看連接到UIView。爲了解決這個問題,你必須拖動UITableView並且視圖必須連接到UITableView

對於你的第二個問題,聲明BOOL作爲

@property(assign,nonatomic) BOOL isFiltered; 
+0

我其實沒有一個xib文件。我應該提到我正在使用故事板。 – chrisjr

+0

@ Junior117爲故事板,使用相同的方法。檢查你的viewcontroller的連接面板 – manujmv

0

我在構建iOS7通用應用程序時遇到了這個問題,愚蠢的錯誤:我只建立了iPhone應用程序的一部分,但有計劃設置爲iPad模擬器。在得到錯誤並在這裏尋找之後,我看到了我的錯誤,將該計劃切換到了iPhone,並且該應用程序使用適當的故事板運行以獲得適當的模擬器。希望有所幫助。

5

我有一個類似的錯誤。我可以用@迪內希的建議,解決這個問題,但我不喜歡這一點,因爲我很害怕,可能會出現一些意想不到的後果。

我發現當我看故事板中的場景層次結構時,我注意到我有這個結構(抱歉,我不知道如何格式化它 - 它應該是一個樹結構):

View Controller 
    View 
    Table View 

當我拿出坐在中間的視圖時,我的問題就消失了。但是,在此之前,您需要刪除視圖與視圖控制器或表視圖之間可能存在的任何插座。您確保這些都消失後,按照下列最後步驟:

  1. 拖動表視圖,以便它是視圖控制器的直系後裔。
  2. 刪除視圖
  3. 命令 - 從視圖控制器拖動到表視圖,從而直接在兩者之間創建一個新的出口。

此外,將.h文件保留爲UITableView(而不是UIView)的子類。

無論如何,這解決了這個問題對我來說。如果有人遇到這個問題,我希望它有幫助。

+0

奇怪的是,它也解決了我的問題。看起來像內部框架之間的一個可怕的誤解!感謝提示,這非常有見地。 –

相關問題