UISearch 栏总是显示:没有结果

Posted

技术标签:

【中文标题】UISearch 栏总是显示:没有结果【英文标题】:UISearch bar always displays : no result 【发布时间】:2013-04-04 20:09:28 【问题描述】:

在我的应用程序中,我在uitableviewsuccessfully 中显示来自 sqlite 数据库的数据,并插入 UISearchbardisplay controller 以在另一个表中显示搜索结果,问题是当我键入要搜索的字符串时,它总是return : 即使该字符串存在于该表中也没有结果!!

我认为我的方法有问题 (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;

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

我把我的代码改成了这个,如果有一天有人需要它,它可以在这里正常工作:

- (void)viewDidLoad 

maListe = [[NSMutableArray alloc] init];
tampon = [[NSMutableArray alloc] init];
int i;
for (i=0 ; i <= 4; i++) 

 MasterViewControllerAppDelegate* appDelegate=(MasterViewControllerAppDelegate*)[[UIApplication sharedApplication]delegate];
SingleStudent* sStudent=[appDelegate.aryDatabase objectAtIndex:i];

[maListe addObject:[sStudent strName]];



    
[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];
//NSString *cellValue = [sStudent strName];
NSString *cellValue = [maListe objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// Configure the cell.
/*NSString *cellValue = [maListe objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;*/


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(NSString *name in tampon2)



            NSRange r = [name rangeOfString:searchText];
    if(r.location != NSNotFound)
    
        if(r.location== 0) // premiere lettre
        [maListe addObject:name];
    


【讨论】:

以上是关于UISearch 栏总是显示:没有结果的主要内容,如果未能解决你的问题,请参考以下文章

IOS RxSwift中UISearch栏文本和视图模型属性之间的两种方式绑定

Select2 不工作,总是显示“未找到结果”

使用 Swift 搜索结果后,搜索栏没有重新加载到原始数据?

如何在导航栏内隐藏 UISearchController

UISearch 在 JSON api UITableView 中不起作用

突出显示搜索词