加载了nib文件,但没有得到UITableView。

4

我一直在跟随这个YouTube教程(第1部分第2部分)。

我已经完成了这两个视频,并使用以下代码将视图控制器与父视图控制器连接:

- (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.'

我正在使用storyboards制作这个应用程序。
我是否遗漏了什么?谢谢您提前。
另外一个问题:每当isFiltered == YES时,我也会收到一个警告,即“指针和整数之间的比较('BOOL *'(又名'signed char *')和'int')。有没有办法解决它?
下面是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

注意:我进行了一些编辑以适应我的需求。


请查看此链接:https://dev59.com/1Ggu5IYBdhLWcg3wln8O - Dinesh
@Dinesh 那实际上是我在问这个问题前去过的第一个,但它没有帮到我。 - chrisjr
我在这里发布了一个答案https://dev59.com/1Ggu5IYBdhLWcg3wln8O,希望能对你有所帮助,如果确实有帮助,请投票支持。谢谢! - uplearned.com
5个回答

19

你试过将 @interface SearchViewController : UITableViewController 改为 @interface SearchViewController : UIViewController 吗?

我强烈怀疑你没有在XIB中将UITableview附加为视图,或者你的类应该派生自UIViewController而不是UITableviewController类。


1
很奇怪,你的建议起作用了。虽然它明显有一个UITableView,但我不太确定为什么它不接受UITableViewController,但它确实有效。谢谢你的帮助。 - chrisjr

6

我曾经也遇到过类似的错误。我使用了@Dinesh的建议解决了这个问题,但我不太喜欢这种方法,因为我担心可能会有一些意外后果。

后来我发现,当我查看故事板中的场景层次结构时,我注意到我有这样的结构(抱歉,我不知道如何格式化它 - 它应该是一个树形结构):

View Controller
  View
     Table View

当我删除了位于中间的视图时,我的问题消失了。但在此之前,您需要删除可能存在于视图和视图控制器或表格视图之间的任何插座。确保这些已经消失后,请按照以下最终步骤操作:
  1. 拖动表格视图,使其成为视图控制器的直接子代。
  2. 删除视图
  3. 从视图控制器向表格视图进行Command-Drag,从而在两者之间直接创建一个新插座。
此外,将.h文件保留为UITableView的子类(而不是UIView)。
总之,这对我解决了问题。如果有人遇到这个问题,我希望它能有所帮助。

很奇怪,这也解决了我的问题。似乎是内部框架之间的可怕误解!感谢您的提示,非常有见地。 - M. Porooshani

2

针对您的第一个问题,您需要检查 storyboard。我相信您的 file owner'sview 已经连接到了一个 UIView。要解决这个问题,您必须拖动 UITableView 并将视图连接到 UITableView

针对您的第二个问题,请声明 BOOL

@property(assign,nonatomic) BOOL isFiltered;

我实际上没有xib文件。我应该提到我正在使用storyboards。 - chrisjr
@Junior117,对于Storyboard,请使用相同的方法。检查您的视图控制器的连接面板。 - manujmv

2

关于您提出的警告问题,这是因为您将BOOL isFiltered作为指针进行了定义。


0
在构建一个简单的iOS7通用应用程序时,我遇到了一个愚蠢的错误:我只构建了iPhone应用程序的一部分,但将方案设置为iPad模拟器。在看到这里的错误后,我发现了我的错误,将方案切换到iPhone,并使用适当的故事板运行了应用程序。希望这可以帮助到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接