加载了笔尖,但没有得到UITableView

时间:2013-11-27 05:50:35

标签: ios objective-c uitableview uisearchbar

我一直在YouTube上关注本教程(part 1part 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.'

我正在为此应用使用Storyboard。

有什么东西我不见了吗?提前谢谢。

一个附带问题:我也会收到警告,只要显示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

注意:我做了一些编辑以满足我的需求。

5 个答案:

答案 0 :(得分:19)

您是否尝试将@interface SearchViewController : UITableViewController更改为@interface SearchViewController : UIViewController

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

答案 1 :(得分:6)

我有类似的错误。我能够使用@Dinesh的建议来解决它,但我不喜欢这样,因为我担心会有一些意想不到的后果。

我想到的是,当我查看故事板中的场景层次结构时,我注意到我有这种结构(抱歉,我不知道如何格式化它 - 它应该是树结构):< / p>

View Controller
  View
     Table View

当我拿出位于中间的View时,我的问题就消失了。但是,在执行此操作之前,您需要删除视图与视图控制器或表视图之间可能存在的任何插座。在确定这些消失后,请按照以下最后步骤操作:

  1. 拖动表视图,使其成为View Controller的直接后代。
  2. 删除视图
  3. 命令 - 从视图控制器拖动到表视图,从而直接在两者之间创建新的插座。
  4. 另外,将.h文件保留为UITableView的子类(不是UIView)。

    无论如何,这解决了我的问题。如果有人遇到这个,我希望它有所帮助。

答案 2 :(得分:2)

对于有关警告的问题,警告之所以发出警告,是因为您已将BOOK过滤为指针。

答案 3 :(得分:2)

对于您的第一个问题,您需要查看故事板。我确信您的文件所有者 视图已连接到 UIView 。要解决此问题,您必须拖动 UITableView ,并且必须将视图连接到 UITableView

对于您的第二个问题,请将 BOOL 声明为

@property(assign,nonatomic) BOOL isFiltered;

答案 4 :(得分:0)

我在构建一个简单,愚蠢的错误的iOS7 Universal应用程序时遇到了这个问题:我只构建了部分iPhone应用程序,但将方案设置为iPad模拟器。在收到错误并看到这里后,我看到了我的错误,将方案切换到了iPhone,并且应用程序运行了适当的故事板以供正确的模拟器使用。希望有所帮助。