- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我成功显示了uitableview
中来自sqlite数据库的数据,并插入了UISearchbar
和display controller
以在另一个表中显示搜索结果,问题是当我键入要搜索的字符串时,它总是返回:no结果,即使该字符串存在于该表中!
我认为我的方法有问题
(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];}
}}
#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];
}
-(id)initWithData:(int)pK :(int)aId :(NSString*)name`
{
self.intPK = pK;
self.intId = aId;
self.strName = name;
return self;}
最佳答案
我将代码更改为此,如果某天有需要,可以在这里进行更改:
- (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];
}
}
}
关于xcode - UISearch栏始终显示:无结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820772/
我正在从解析中查询,当我尝试使用搜索栏进行搜索时,它返回“无结果”。我试图弄清楚我在显示对象时做错了什么,因为它确实在 NSLog 中显示它找到了对象。 我的 viewDidLoad: - (v
当我开始编辑搜索栏内容时,我试图扩展 UISearchBar 的高度。我正在 UISearchBarController 中执行以下代码。 func searchBarTextDidBeginEdit
我有一个预填充“当前位置”的 UISearch 栏,但是当用户开始输入时,我想删除预填充的文本,只显示用户正在输入的文本。我已经尝试过了,但似乎无法正常工作: -(void)searchBar:(UI
我正在使用 uisearchbar、tableview 和 JSON Web 服务使用以下代码进行搜索。当前的问题是,尽管应用程序正在搜索并在表格 View 中呈现数据,但它是在搜索栏中发生每个文本更
我有一个带有 uisearchbar 和 uitable 的简单 View Controller 。我的问题是,当点击搜索栏时,我看到委托(delegate)函数 searchBarShouldBeg
在我的 Storyboard上,我添加了一个 UISearchBar,并在其下方添加了一个表格 View 。在 iPhone 8 上,我看到 UISearchBar 位于导航栏/标题的正下方;在 iP
用于呈现 SearchController 的代码: searchController = UISearchController(searchResultsController:viewcontrol
我需要实现搜索历史记录。所以我想做的是在尚未输入文本时在搜索显示 Controller 中显示历史记录。 我实现了这个: -(void)searchDisplayControllerDidBeginS
我试图在 swift 4 中使搜索栏透明。 这是我的代码 let searchBar = UISearchBar() searchBar.sizeToFit() search
我只是想知道在 iOS 应用程序中是否可以将 UISearchBar 或 UISearchDisplayController 设置为 UIActionSheet? 最佳答案 UIActionSheet
我在一个社交网络上工作,我希望用户名只使用小写,在用户名文本字段中,我如何让键盘不允许大写字母(我可以在事后强制 textfield.text 字母变成小写但我真的不想那样做)我还需要它为 UISea
我正在尝试在我的应用程序中配置我的搜索栏。我有一个包含 NSObject 实例的数组,并尝试使用搜索选项来查找(这个 NSObjects 的)合适的属性。其实,我可以找到它。我的问题是,我只能在 UI
我是一名优秀的程序员,十分优秀!