gpt4 book ai didi

ios - 在 UITableView 中过滤 NSMutableDictionary 数据

转载 作者:行者123 更新时间:2023-11-29 03:58:57 27 4
gpt4 key购买 nike

我需要通过 UISearchbar 中输入的文本过滤 UITableview 中的数据。我关注了this example但 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];

我的 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;
}

我的搜索:

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

}

最佳答案

首先,您需要为 Controller /数据源创建一个状态/标志,以便它知道您处于搜索/过滤模式下的天气。

然后,如果您处于搜索模式,请将数据源方法指向filteredArray。

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

}

希望大家理解。

关于ios - 在 UITableView 中过滤 NSMutableDictionary 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16130926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com