2

私のアプリケーションでは、sqliteデータベースからのデータをuitableview正常に表示し、別のテーブルに検索結果を挿入UISearchbardisplay controllerて表示します。問題は、検索する文字列を入力すると、常に返されることです:この文字列がそのテーブルに存在する場合でも結果はありません!!

メソッドに問題があると思います (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTextが、何が問題なのか、どのように対処すればよいのか正確にはわかりません!! これが私のコードです:

MasterViewControllerViewController.m :

#import "MasterViewControllerViewController.h"
#import"MasterViewControllerAppDelegate.h"

#import"SingleStudent.h"


- (void)viewDidLoad {

MasterViewControllerAppDelegate* appDelegate =(MasterViewControllerAppDelegate*)[[UIApplication sharedApplication]delegate];
maListe = [[NSMutableArray alloc] init];
tampon = [[NSMutableArray alloc] init];
[maListe addObjectsFromArray:appDelegate.aryDatabase];
[tampon addObjectsFromArray:maListe];
 [super viewDidLoad];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


{static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MasterViewControllerAppDelegate* appDelegate =(MasterViewControllerAppDelegate*)[[UIApplication sharedApplication]delegate];
SingleStudent* sStudent =[appDelegate.aryDatabase objectAtIndex:indexPath.row];

cell.textLabel.text = [sStudent strName];
// Configure the cell.

return cell;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

tampon2 = [[NSMutableArray alloc] init];


[tampon2 addObjectsFromArray:tampon];
[maListe removeAllObjects];
if([searchText isEqualToString:@""])
{
    [maListe removeAllObjects];
    [maListe addObjectsFromArray:tampon]; // Restitution des données originales
    return;
}


for (NSDictionary *dict in tampon2 ){
      NSString *name = [NSString stringWithFormat:@"%@", dict];



        NSRange  range = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound)
        { 
            if(range.location== 0) // premiere lettre
                [maListe addObject:name];}
        }}

MasterViewControllerAppDelegate.m

#import "MasterViewControllerAppDelegate.h"
#import "MasterViewControllerViewController.h"
#import "SingleStudent.h"

-(void)updateNames {

databaseName = @"students7.sqlite";
NSArray* documentsPath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDir =[documentsPath objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];}

-(void)checkAndCreateDatabase {

BOOL success;
NSFileManager* fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if (success) {
    return;
}
NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}


-(void)readWordsFromDatabase{     



    db = [FMDatabase databaseWithPath:databasePath];
aryDatabase = [[NSMutableArray alloc] init];
[db setLogsErrors:TRUE];
[db setTraceExecution:TRUE];
if (![db open]) {
    NSLog(@"failed to open database");
    return;
}
else {
    NSLog(@"Opened database successfully");
}
FMResultSet* rs = [db executeQuery:@"SELECT * FROM student"];
while ([rs next]) {
    int aID = [rs intForColumn:@"id"];
    int aPK = [rs intForColumn:@"pk"];
    NSString* aName = [rs stringForColumn:@"name"];

    SingleStudent* sStudent = [[SingleStudent alloc] initWithData:aPK :aID :aName];
    [aryDatabase addObject:sStudent];
    [sStudent release];

}
[db close];

}

singleStudent.m

-(id)initWithData:(int)pK :(int)aId :(NSString*)name`
{

self.intPK = pK;
self.intId = aId;
self.strName = name;
return self;}

この問題を解決するにはどうすればよいですか?

4

1 に答える 1