1

我创建了一个带有 searchBar 的 UITextView 的子类,代码如下:

#import "SezioniTableController.h"

@interface SezioniTableController ()

@end

@implementation SezioniTableController

@synthesize searchBar,searchDisplayController;
@synthesize arraySezioni,arrayFiltrato;
@synthesize objTesto;

- (id)initWithStyle:(UITableViewStyle)style andArray:(NSMutableArray *)array{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization

        self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        self.searchBar.delegate = self;

        self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
        self.searchDisplayController.delegate = self;
        self.searchDisplayController.searchResultsDataSource = self;

        self.arraySezioni = array;
        self.arrayFiltrato = array;

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) loadView {}

#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.

    return self.arrayFiltrato.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"MyIdentifier"];
    }

    self.objTesto = [arrayFiltrato objectAtIndex:indexPath.row];

    cell.textLabel.text = self.objTesto.titoloTesto;

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */

    NSDictionary *dict = [NSDictionary dictionaryWithObject:[self.arrayFiltrato objectAtIndex:indexPath.row] forKey:@"Testo"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"PassaggioTesto" object:self userInfo:dict];
        NSLog(@"Click");
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    return self.searchBar; //in .h, IBOutlet UISearchBar* search;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 44;
}

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    [self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    NSLog(@"aa");

    //[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:self];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{

    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    NSLog(@"ab");

    //[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:self];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void) filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{

    NSLog(@"a count:%d",self.arraySezioni.count);

    [self.arrayFiltrato removeAllObjects]; // First clear the filtered array.

    for (objTesto in self.arraySezioni){
        NSComparisonResult result = [objTesto.titoloTesto compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
        if (result == NSOrderedSame){
            NSLog(@"Titolo: %@",objTesto.titoloTesto);
            [self.arrayFiltrato addObject:self.objTesto];
        }else{
            NSLog(@"Non trovato");
        }
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)saearchBar {
    [self.arrayFiltrato removeAllObjects];
    [self.arrayFiltrato addObjectsFromArray: self.arraySezioni];
}

@end

在我的 uiviewcontroller 上,我使用以下代码创建的 uitableview 创建了一个表:

self.arrayTesti = [self.dataLoad leggiArgomentiDellaSezione:tag];

self.table = [[SezioniTableController alloc] initWithStyle:UITableViewStylePlain andArray:self.arrayTesti];

self.tableArgumentView.delegate = self.table;
self.tableArgumentView.dataSource = self.table;

[self.tableArgumentView reloadData];

视图控制器适用于 iPad 应用程序,该表可以完美运行,但如果我尝试搜索某些内容,它就无法正常工作!
你能帮助我吗?

4

1 回答 1

0

如果你分配

self.arraySezioni = array;
self.arrayFiltrato = array;

那么两者self.arraySezioniself.arrayFiltrato都是指向同一个数组的指针。这意味着如果你清空一个数组

[self.arrayFiltrato removeAllObjects];

那么另一个数组self.arraySezioni(和原来的array)也是空的。

所以至少self.arrayFiltrato应该是原始数组的副本:

 self.arrayFiltrato = [array copy];
于 2012-11-14T10:49:50.283 回答