搜索搜索栏中的数组

时间:2015-05-07 09:55:15

标签: ios objective-c arrays xcode search

我参考了本文的advantage来在我的应用程序中创建搜索。稍微改变了自己的代码。但是,如果我添加多个值的数组(1-5),搜索工作正常,但如果你添加一个15值的数组,那么错误就会消失

    2015-05-07 12:24:55.852 gfhfgh[5662:60b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2935.138/UITableView.m:5439
2015-05-07 12:24:55.860 gfhfgh[5662:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier SearchResultsTableViewUITableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(0x2e2fdf83 0x38aaeccf 0x2e2fde5d 0x2ecabd5b 0x30c86dff 0xf00b9 0x30c4d8f7 0x30bf4c27 0x30be7c5d 0x30be7ba3 0x30bf41cd 0x30b3a167 0x30be71eb 0x30bc86fd 0x30bc848d 0x30bc9bf5 0x30c7dcc5 0x30d0c011 0x30c36109 0x30d0abcd 0x30d0aaaf 0x30b50037 0x30b4ffd7 0x30b4ffb1 0x30b3b717 0x30b3acfd 0x30b37805 0x30c16f15 0x30d0a91f 0x30b99127 0x30b993e7 0x30c15c17 0x30e9e579 0x30ca0fed 0x30b4c4e3 0x30ef3aed 0x30b13353 0x30b11a9b 0x30b4ad49 0x30b4a66f 0x30b1f8cd 0x30b1df77 0x2e2c920b 0x2e2c86db 0x2e2c6ecf 0x2e231ebf 0x2e231ca3 0x33137663 0x30b7e14d 0xfa1a9 0x38fbbab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

有什么问题? 我的代码

        #import "StreetTableViewController.h"
    @interface StreetTableViewController () 
    //@interface StreetTableViewController () <UISearchDisplayDelegate>
    // the items to be searched

    @property (nonatomic, copy) NSArray *items;

    // the current search results
    @property (nonatomic, copy) NSArray *searchResults;

    @end

    @implementation StreetTableViewController

    #pragma mark - NSCoding

    // set some initial searchable items
    - (instancetype)initWithCoder:(NSCoder *)coder
    {
        self = [super initWithCoder:coder];
        if (self) {
            _items
                     = [NSArray arrayWithObjects: @"bar", @"foo", @"quux",@"bar2", @"foo2", @"quux2",@"bar3", @"foo3", @"quux3",@"bar4", @"foo4", @"quux4",@"bar5", @"foo5", @"quux5",@"bar6", @"foo6", @"quux6",
@"bar7", @"foo7", @"quux7",@"bar8", @"foo8", @"quux8",@"bar9", @"foo9", @"quux9",@"bar10", @"foo10", @"quux10",  nil];
        }
        return self;
    }

    #pragma mark - UISearchDisplayDelegate

    // register a cell reuse identifier for the search results table view
    -(void)searchDisplayController:(UISearchDisplayController *)controller
     didLoadSearchResultsTableView:(UITableView *)tableView {
        [tableView registerClass:[UITableViewCell class]
          forCellReuseIdentifier:@"SearchResultsTableViewUITableViewCell"];
    }

    // perform the search
    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller
    shouldReloadTableForSearchString:(NSString *)searchString {
        NSPredicate *predicate
        = [NSPredicate predicateWithFormat:@"self beginswith [c] %@", searchString];
        NSArray *searchResults
        = [[self items] filteredArrayUsingPredicate:predicate];
        [self setSearchResults:searchResults];

        return YES;
    }

    #pragma mark - UITableViewDataSource

    // check if displaying search results
    -(NSInteger)tableView:(UITableView *)tableView
    numberOfRowsInSection:(NSInteger)section {
        if ([[self searchDisplayController] isActive]) {
            return [[self searchResults] count];
        } else {
            return [[self items] count];
        }
    }

    // check if displaying search results
    -(UITableViewCell *)tableView:(UITableView *)tableView
            cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if ([[self searchDisplayController] isActive]) {
            UITableViewCell *cell
            = [tableView dequeueReusableCellWithIdentifier:@"SearchResultsTableViewUITableViewCell"
                                              forIndexPath:indexPath];
            id item = [[self searchResults] objectAtIndex:[indexPath row]];
            [[cell textLabel] setText:item];
            return cell;
        } else {
            UITableViewCell *cell
            = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
                                              forIndexPath:indexPath];
            id item = [[self items] objectAtIndex:[indexPath row]];
            [[cell textLabel] setText:item];
            return cell;
        }
    }

    #pragma mark - UITableViewDelegate

    // manually perform detail segue after selecting a search result
    -(void)tableView:(UITableView *)tableView
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if ([[self searchDisplayController] isActive]) {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            [self performSegueWithIdentifier:@"detailSegue" sender:cell];
        }
    }

    #pragma mark - UIViewController

    /* prepare for detail scene segue
     called after cell selection in the master and
     search results table views */
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        UITableViewCell *cell = (UITableViewCell *)sender;

        id item = nil;
        if ([[self searchDisplayController] isActive]) {
            NSIndexPath *indexPath
            = [[[self searchDisplayController] searchResultsTableView] indexPathForCell:cell];
            item = [[self searchResults] objectAtIndex:[indexPath row]];
        } else {
            NSIndexPath *indexPath
            = [[self tableView] indexPathForCell:cell];
            item = [[self items] objectAtIndex:[indexPath row]];
        }

        UIViewController *detail
        = (UIViewController *)[segue destinationViewController];
        [[detail navigationItem] setTitle:item];
    }
    @end

1 个答案:

答案 0 :(得分:2)

我可能理解你的问题(或者可能不是......)。检查您是否在SearchResultsTableViewUITableViewCell.nib文件(或SearchResultsTableViewUITableViewCell.m文件)中编写了单元格标识符(SearchResultsTableViewUITableViewCell)。

我认为你根本没有重复使用细胞。对于你不需要的5个单元格,因为UITableView无论如何都需要创建5个单元格(dequeue方法create(tableHeight / cellHeight)单元格,并且所有其他单元格都被重用)。

另一种选择,如果您注册了您的类而不是您的nib文件: 添加

[self.yourTableView registerNib:[UINib nibWithNibName:@"SearchResultsTableViewUITableViewCell" bundle:nil] forCellReuseIdentifier:@"SearchResultsTableViewUITableViewCell"];