ios - 在搜索栏中搜索数组

标签 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

最佳答案

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

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

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

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

关于ios - 在搜索栏中搜索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30097610/

相关文章:

php - 在模型中获取值使用 asArray Yii2

c++ - 如何让 tr1::array 分配对齐内存?

ios - 更改 MKMapView 用户位置注释

ios - iPhone MKMapView 中的注释聚类

iOS:有干净的代码,使用静态类或单例

ios - viewWithTag:reloadData 后返回 nil

iphone - 如何关闭内容 View 中的弹出 View ?

ios - NSXMLParser 找不到结束标记

ios - 从关系中获取请求 CoreData

java - 整数数组静态初始化