0

UITableviewに入力されたテキストでデータをフィルタリングする必要がありますUISearchbar私はこの例に従いましたが、そこにデータがあり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;
}


- (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;
}

私の検索:

#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];

}
4

1 に答える 1

1

まず、コントローラー/データ ソースの状態/フラグを作成して、検索/フィルター モードになっている天気を認識できるようにする必要があります。

次に、検索モードの場合は、データ ソース メソッドをfilteredArrayに向けます。

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

}

それが理解されることを願っています。

于 2013-04-21T12:13:58.283 に答える