简体   繁体   English

在字典数组中搜索并在表视图中显示数据

[英]Searching in Array of dictionary and displaying data in table view

I have an array of dictionaries where I what to implement search functionality and display data on tableView cells. 我有一系列字典,在其中可以实现搜索功能并在tableView单元格上显示数据。

Search works quite fine, I have tested it: 搜索效果很好,我已经对其进行了测试:

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

    NSString *dictionryKey = @"eViela";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS [cd] %@", dictionryKey, searchText];
    NSArray *filteredArray = [self.allItems filteredArrayUsingPredicate:predicate];
    NSLog(@"%@", filteredArray);

    self.sortedText = filteredArray;

    [self.tableView reloadData];

}

Now I need tableView to fill data only for key "eViela" while user is typing info in search bar. 现在,当用户在搜索栏中键入信息时,我需要tableView仅填充键“ eViela”的数据。 As of now I can show only all items as per below code: 到目前为止,我只能按照以下代码显示所有项目:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
    EvielasLibrary *library = [[EvielasLibrary alloc] init];
    cell.textLabel.text = [[library.library objectAtIndex:indexPath.row] 
    return cell;
}

I know that it should be easy but I can't figure it out. 我知道这应该很容易,但我无法弄清楚。

Thanks for help! 感谢帮助!

You need to get Model from array instead of create new one . 您需要从数组中获取模型,而不是创建新模型。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
    EvielasLibrary *library = [self.sortedText objectAtIndex:indexPath.row]; // assume that  self.sortedText is your data source array of tableview
    cell.textLabel.text = @"" // populate your string here  
    return cell;
}
NSMutableArray *arrayForTableView = [[NSMutableArray alloc] init];
NSString *dictionryKey = @"eViela";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",dictionryKey];
NSArray *filteredArray = [self.allItems filteredArrayUsingPredicate:predicate];
for (NSInteger i = 0; i<filteredArray.count; i++){
    NSString *matchValue = [NSString stringWithFormat:@"%@",[filteredArray objectAtIndex:i]];
    if ([matchValue isEqualToString:dictionryKey]){
        [arrayForTableView addObject:matchValue];
    }
}

Use arrayForTableView array for your tableview 将arrayForTableView数组用于表视图

cell.textLabel.text = [arrayForTableView objectAtIndex:indexPath.row];

Problem is that you not updating numberOfRowsInSection delegate method for sortedArray 问题是您没有更新sortedArray的numberOfRowsInSection委托方法

You need to check if search bar is active or not. 您需要检查搜索栏是否处于活动状态。 if search bar is active then use sortedText array else if search bar is not active then use library.library array 如果搜索栏处于活动状态,则使用sortedText数组;否则,如果搜索栏处于非活动状态,则使用library.library数组

For check search bar active create a global variable isSearchBarActive And assign it true value in searchBarTextDidBeginEditing delegate function and assign false in searchBarTextDidEndEditing 对于选中的搜索栏,请创建一个全局变量isSearchBarActive并在searchBarTextDidBeginEditing委托函数中为其分配true值,并在searchBarTextDidEndEditing false分配给false

Add Condition in numberOfRowInSection numberOfRowInSection添加条件

if searchBarActive {
    return self.sortedText.count
}
else {
    return library.library.count
}

Add Condition in cellForRowAtIndexPath like this 像这样在cellForRowAtIndexPath添加条件

if searchBarActive {
    cell.textLabel.text = [self.sortedText objectAtIndex:indexPath.row] 
}
else {
    cell.textLabel.text = [library.library objectAtIndex:indexPath.row] 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM