我有3 NSMutableArray
个相同的大小。他们是"链接"这意味着对于相应的索引,它们彼此相关。
tableData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", nil]
thumbnails = [NSMutableArray arrayWithObjects:@"egg_benedict.jpg", @"mushroom_risotto.jpg", @"full_breakfast.jpg",nil]
prepTime = [NSMutableArray arrayWithObjects:@"10min", @"15min", @"8min",nil]
这来自我正在玩的教程。
我正在过滤tableData数组,如下所示:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResultsData = [[tableData filteredArrayUsingPredicate:resultPredicate] mutableCopy];
其中searchText
是包含过滤器的字符串(例如" egg")。
这很好用,我的意思是我有正确的过滤。 (searchResultsData
是另一个NSMutableArray)
我需要做的是根据从上面NSMutableArray
获得的结果过滤其他两个NSPredicate
。
所以我创建了另外两个NSMutableArray
s,名为" searchResultThumbnails"和" searchResultPrepTime
"。
我期待这个:如果我过滤使用" egg"我想要第一个元素包含" egg"来自" tableData
"数组(在这种情况下只有一个元素)和缩略图和预处理数组中索引处的对应元素。
用" Egg"过滤后结果应该是:
searchResultData = "Egg"
searchResultThumbnails = "egg_benedict.jpg"
searchResultPrepTime = "10min"
感谢您的帮助。
答案 0 :(得分:0)
相信“他们是”联系在一起的“这意味着对于相应的索引,他们有相互关联的东西。”作为你的情况
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResultsData = [[tableData filteredArrayUsingPredicate:resultPredicate] mutableCopy];
NSString *searchedText = [searchResultsData objectAtIndex:0];
NSInteger index = [tableData indexOfObject:searchedText]; //if searchedText = "Egg"
NSString *thumb = [thumbnails objectAtIndex:index];
NSString *prep= [prepTime objectAtIndex:index];
但这不是更好的方法。
你有很多选择 您可以使用可能具有属性项,缩略图,prepTime的自定义类。
您还可以使用类似于以下格式的词典数组
array = (
{
searchResultData = "Egg"
searchResultThumbnails = "egg_benedict.jpg"
searchResultPrepTime = "10min"
}
{
searchResultData = "someItem"
searchResultThumbnails = "some.jpg"
searchResultPrepTime = "10min"
}
)
答案 1 :(得分:0)
试试这个:
NSArray* tableData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", nil];
NSArray* thumbnails = [NSMutableArray arrayWithObjects:@"egg_benedict.jpg", @"mushroom_risotto.jpg", @"full_breakfast.jpg",nil];
NSArray* prepTime = [NSMutableArray arrayWithObjects:@"10min", @"15min", @"8min",nil];
NSMutableArray *storedIndex = [NSMutableArray arrayWithCapacity:tableData.count];
for (NSUInteger i = 0 ; i != tableData.count ; i++) {
[storedIndex addObject:[NSNumber numberWithInteger:i]];
}
//Now you are going to sort tabledata.. with it we will sort storedIndexs
//suppose we will compare the strings for this time
[storedIndex sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
NSString *lhs = [[tableData objectAtIndex:[obj1 intValue]] lowercaseString];
NSString *rhs = [[tableData objectAtIndex:[obj2 intValue]] lowercaseString];
return [lhs compare:rhs];
}]; //now storedIndex are sorted according to sorted tableData array
NSMutableArray *sortedTableData = [NSMutableArray arrayWithCapacity:tableData.count];
NSMutableArray *sortedThumbnail = [NSMutableArray arrayWithCapacity:tableData.count];
NSMutableArray *sortedPrepTime = [NSMutableArray arrayWithCapacity:tableData.count];
[p enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSUInteger pos = [obj intValue];
[sortedTableData addObject:[tableData objectAtIndex:pos]];
[sortedThumbnail addObject:[thumbnails objectAtIndex:pos]];
[sortedPrepTime addObject:[prepTime objectAtIndex:pos]];
}];
//Now all will be correct index relation to each other as previous
它会完美运作。 快乐的编码。 :)