0

运行我的应用程序时出现以下错误,

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UITableViewController loadView] 加载了“2-view-3”笔尖,但没有获得 UITableView。

以下是我正在使用的代码,

视图控制器.h

@interface ViewController : UITableViewController <UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSArray *arForTable;
@property (nonatomic,strong) NSArray *arForSearch;

视图控制器.m

@implementation ViewController

@synthesize arForTable = _arForTable;
@synthesize arForSearch = _arForSearch;

-(void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.arForTable = [NSArray arrayWithObjects:
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Pending",@"name",@"2",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Pent",@"name",@"22",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Pen",@"name",@"5",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Neon",@"name",@"7",@"value",nil],
                       [NSDictionary dictionaryWithObjectsAndKeys:@"Spark",@"name",@"99",@"value",nil],
                       nil];
    self.arForSearch = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (tableView == self.tableView)?self.arForTable.count:self.arForSearch.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

    NSDictionary *dToAccess = (self.tableView==tableView)?[self.arForTable objectAtIndex:indexPath.row] : [self.arForSearch objectAtIndex:indexPath.row];
    [(UILabel*)[cell viewWithTag:1] setText:[dToAccess valueForKey:@"name"]];
    [(UILabel*)[cell viewWithTag:2] setText:[dToAccess valueForKey:@"value"]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)filterContentForSearchText:(NSString*)str{
    // for inCaseSensitive search
    str = [str uppercaseString];

    NSMutableArray *ar=[NSMutableArray array];
    for (NSDictionary *d in self.arForTable) {
        NSString *strOriginal = [d valueForKey:@"name"];
        // for inCaseSensitive search
        strOriginal = [strOriginal uppercaseString];

        if([strOriginal hasPrefix:str]) {
            [ar addObject:d];
        }
    }
    self.arForSearch=[NSArray arrayWithArray:ar];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

提前致谢

4

2 回答 2

1

我没有找到 UITableView 或 UISearchBar 的任何出口,而且您继承了 UITableViewController 并实现了不需要的委托和数据源。此外,要拥有 UISearchBar 您将需要创建一个视图控制器来保存表格视图和uisearchbar 因为不建议在 tableview 上添加搜索栏,也不应该这样做。因此,拥有一个搜索栏委托表明您已尝试在 tableview 中实现搜索栏。

如果您需要在uitableviewcontroller 中添加搜索栏,您可以在navigationcontroller 上添加一个按钮。请通过本教程进行检查。

UISearchBar 教程

于 2012-05-11T10:21:39.790 回答
0

由于你的类是 UITableViewController 类型,你不需要实现 UITableViewDataSource 和 UITableViewDelegate 接口。此外,您需要在界面构建器中有一个 UITableView 对象,并且它应该作为类的视图出口链接。然后只会加载屏幕并运行

希望这可以帮助您消除问题。

于 2012-05-11T10:11:24.933 回答