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

chr*_*sjr 3 objective-c uitableview uisearchbar ios

我一直在YouTube上关注本教程(第1 部分第2部分).

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

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

这段代码确实有效,因为这与我用于其他模态视图控制器的格式相同,所以我知道这不是问题.

无论如何,当我点击视图控制器中的搜索按钮时,它应该会弹出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.'
Run Code Online (Sandbox Code Playgroud)

我正在为这个应用程序使用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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

Din*_*esh 19

你试过改变你@interface SearchViewController : UITableViewController@interface SearchViewController : UIViewController

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


And*_*Dev 6

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

我想到的是,当我查看故事板中的场景层次结构时,我注意到我有这个结构(对不起,我不知道如何格式化它 - 它应该是一个树结构):

View Controller
  View
     Table View
Run Code Online (Sandbox Code Playgroud)

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

  1. 拖动表视图,使其成为View Controller的直接后代.
  2. 删除视图
  3. 命令 - 从视图控制器拖动到表视图,从而在两者之间直接创建新的插座.

另外,将.h文件保留为UITableView(非UIView)的子类.

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