加载了笔尖但没有得到 UITableView [英] Loaded the nib but didn't get a UITableView
问题描述
我一直在 YouTube 上关注本教程(第 1 部分 和 第 2 部分).
I have been following this tutorial on YouTube (part 1 and part 2).
我已经完成了两个视频,并使用以下代码将视图控制器与父视图控制器连接起来:
I have completed both videos and have hooked up the view controller with the parent view controller using this code:
- (IBAction)searchButtonClicked:(id)sender {
NSLog(@"It works.");
SearchViewController *searchViewControl = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchControllerNav"];
[self presentViewController:searchViewControl animated:YES completion:nil];
}
这段代码确实有效,因为这与我用于其他模态视图控制器的格式相同,所以我知道这不是问题.
This code indeed works since this is the same format that I use for my other modal view controllers, so i know that's not the problem.
无论如何,当我点击视图控制器中的搜索按钮时,它应该会弹出SearchViewController
.但是,应用程序崩溃了,它给了我这个错误消息:
Anyway, when I tap on the search button in the view controller, it should pop up the SearchViewController
. However, the app crashes instead and it gives me this error message:
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.'
我正在为此应用使用故事板.
I am using Storyboards for this app.
有什么我遗漏的吗?提前致谢.
Is there something that I'm missing? Thank you in advance.
附带问题:我也收到警告,说指针和整数之间的比较('BOOL *'(又名'signed char *')和'int')
每当isFiltered == YES
显示.有什么办法可以解决吗?
A side question: I'm also getting a warning, saying Comparison between pointer and integer ('BOOL *' (aka 'signed char *') and 'int')
whenever isFiltered == YES
is shown. Is there anyway to fix it?
这是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
注意:为了满足我的需要,我做了一些修改.
NOTE: There are a few edits that I made to fit my needs.
推荐答案
您是否尝试过将 @interface SearchViewController : UITableViewController
更改为 @interface SearchViewController : UIViewController
have you tried changing your @interface SearchViewController : UITableViewController
to @interface SearchViewController : UIViewController
我强烈怀疑您没有将 UITableview 作为视图附加到 XIB 中,或者您的类应该派生 UIViewController 而不是 UITableviewController 类..
I strongly suspect that either you have not attached your UITableview as View in XIB or your class should be derived UIViewController instead of UITableviewController class..
这篇关于加载了笔尖但没有得到 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!