在UITableView中过滤NSMutableDictionary数据 [英] Filter NSMutableDictionary data in UITableView

查看:172
本文介绍了在UITableView中过滤NSMutableDictionary数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过在 UISearchbar 中输入的文本过滤 UITableview 中的数据。我跟着这个例子,但是在 NSMutableArray 中有数据,我不能根据我的要求改变它。我的数据是 NSMutableDictionary



我的数据:

  NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES); 
NSString * path = [[documentPaths lastObject] stringByAppendingPathComponent:@data.plist];
NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile:path];

NSMutableDictionary * resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray * resultArray = [[NSMutableArray alloc] init];
NSDictionary * myDict = [NSDictionary dictionaryWithContentsOfFile:path];

sectionKeys = [NSMutableArray new];
sectionsTitle = [NSMutableArray new];

NSArray * tableArray = [myDict objectForKey:@Black];
[resultArray addObject:@Black];
[resultDic setValue:tableArray forKey:@Black];
[sectionsTitle addObject:[NSString stringWithFormat:@%@,[tableData valueForKey:@Black]]];
[sectionCord addObject:[NSString stringWithFormat:@%@,[tableData valueForKey:@Coordinates]]];
[sectionKeys addObject:@Section1];

self.tableData = resultDic;
self.sectionsTitle = resultArray;

[myTable reloadData];

我的表:

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionKeys.count;


$ b - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionKeys objectAtIndex:section];


$ b - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int rowCount;
if(self.isFiltered)
rowCount = [[filteredTableData objectForKey:[sectionsTitle objectAtIndex:section]] count];

else
rowCount = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count];

return rowCount; (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

静态NSString * CellIdentifier = @单元格 ;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


if(isFiltered){
NSDictionary * dict = [[filteredTableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@%@,[dict objectForKey:@Name]];
cell.detailTextLabel.text = [NSString stringWithFormat:@%@,[dict objectForKey:@Address]];

else {
NSDictionary * dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@%@,[dict objectForKey:@Name]];
cell.detailTextLabel.text = [NSString stringWithFormat:@%@,[dict objectForKey:@Address]];
}


return cell;

$ / code>

我的搜寻:

  #pragma mark  -  SearchBar委托 -  
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text {


if(text.length == 0)
{
isFiltered = FALSE;
NSLog(@false);
}
else
{
isFiltered = true;
NSLog(@true);
filteredTableData = [[NSMutableDictionary alloc] init];
$ b $ for(MyAnnotation * ann in tableData)
{
NSRange nameRange = [ann.title rangeOfString:text options:NSCaseInsensitiveSearch];
NSRange descriptionRange = [ann.description rangeOfString:text options:NSCaseInsensitiveSearch];
if(nameRange.location!= NSNotFound || descriptionRange.location!= NSNotFound)
{
[filteredTableData addObject:ann];
//错误
}
}
}

[myTable reloadData];


$ / code $ / pre

解决方案

所有,你需要为控制器/数据源创建一个状态/标志,以便知道你处于搜索/过滤模式的天气。

然后,如果您可以在搜索模式下将数据源方法指向filteredArray。

$ $ $ $ $ $ $ $ $ $ $ $ - $(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int numberOfSections = 0;
if(_searchMode)
{
numberOfSections = self.filteredDataDict.allKeys.count;
}
else
{
numberOfSections = self.tableData.allKeys.count;
}
return numberOfSections;


$ / code>

希望能够理解。


I need to filter data in UITableview by text entered in UISearchbar. I followed this example but there data in NSMutableArray and I can't alter it under my requirements. My data is NSMutableDictionary. I'm stuck on this for long time.

My data:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];

sectionKeys = [NSMutableArray new];
sectionsTitle = [NSMutableArray new];

NSArray *tableArray = [myDict objectForKey:@"Black"];
[resultArray addObject:@"Black"];
[resultDic setValue:tableArray forKey:@"Black"];
[sectionsTitle addObject:[NSString stringWithFormat:@"%@", [tableData valueForKey:@"Black"]]];
[sectionCord addObject:[NSString stringWithFormat:@"%@", [tableData valueForKey:@"Coordinates"]]];
[sectionKeys addObject:@"Section1"];

self.tableData = resultDic;
self.sectionsTitle = resultArray;

[myTable reloadData];

My table:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return sectionKeys.count;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [sectionKeys objectAtIndex:section];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int rowCount;
    if(self.isFiltered)
        rowCount = [[filteredTableData objectForKey:[sectionsTitle objectAtIndex:section]] count];

    else
        rowCount = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count];

    return rowCount;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


    if(isFiltered){
         NSDictionary *dict = [[filteredTableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        cell.textLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Name"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Address"]];
    }
    else{
         NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        cell.textLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Name"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Address"]];
    }


    return cell;
}

My search:

#pragma mark - SearchBar Delegate -
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text{


    if(text.length == 0)
    {
        isFiltered = FALSE;
        NSLog(@"false");
    }
    else
    {
        isFiltered = true;
        NSLog(@"true");
        filteredTableData = [[NSMutableDictionary alloc] init];

        for (MyAnnotation* ann in tableData)
        {
            NSRange nameRange = [ann.title rangeOfString:text options:NSCaseInsensitiveSearch];
            NSRange descriptionRange = [ann.description rangeOfString:text options:NSCaseInsensitiveSearch];
            if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
            {
                [filteredTableData addObject:ann];
                //error
            }
        }
    }

    [myTable reloadData];

}

解决方案

First of all, you need to create a state/flag for the controller/data source, in order for it to know weather you are in search/filter mode.

Then, if you are in search mode, point the data source methods to the filteredArray.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    int numberOfSections = 0;
    if (_searchMode)
        {
              numberOfSections = self.filteredDataDict.allKeys.count;
        }
        else
        {
              numberOfSections = self.tableData.allKeys.count;
        }
    return numberOfSections;

}

Hope it's understood.

这篇关于在UITableView中过滤NSMutableDictionary数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆