用户输入一些文本后,表格视图将被过滤,结果将显示在屏幕上。我正在尝试从我的plist文件中获取其余数据,如下所示:
<dict>
<key>Things to do</key>
<array>
<dict>
<key>activity</key>
<string>Watch movies or TV shows</string>
<key>keywords</key>
<array>
<string>tv shows</string>
<string>movies</string>
</array>
<key>points</key>
<string>10</string>
</dict>
<dict>
<key>activity</key>
<string>Go Hiking</string>
<key>keywords</key>
<array>
<string>hiking</string>
<string>mountains</string>
</array>
<key>points</key>
<string>50</string>
</dict>
<dict>
<key>activity</key>
<string>Video Games</string>
<key>keywords</key>
<array>
<string>video games</string>
<string>playstation</string>
<string>xbox</string>
<string>nintendo</string>
</array>
<key>points</key>
<string>5</string>
</dict>
</array>
</dict>
假设用户输入“WAT”并且mutablearray searchResult包含“观看电影或电视节目”:
self.searchResults = [NSMutableArray arrayWithArray:[activityArray
filteredArrayUsingPredicate:resultPredicate]];
将初始数据加载到NSArray * tableData中。如何在tableData中搜索匹配项并根据该匹配项获取其余信息?
if ([self.tableData containsObject:searchText]) {
//or containsObject:self.searchResult still gives me a NO in the log
NSLog(@"YES");
}else{
NSLog(@"NO");
}
或类似的东西......
for (int i=0; i< [self.searchResult count]; i++){
if ([self.searchResult objectAtIndex:i] in self.tableData){
NSLog(@"YES");
}
}
我关门了吗?我如何获得关键字值呢?
答案 0 :(得分:0)
希望这个答案可以帮助某人......经过2天的网络搜索。 searchResults是一个MutableArray。我需要为每个活动遍历MutableArray和所有NSDictionaries。然后我把它插入表格单元格中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//basic initialization of table cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//if there is something written in the search area...
if (tableView == self.searchDisplayController.searchResultsTableView) {
//use i to loop through all tableData dictionaries
//use j to loop through how many possible matches your search found
int i=0; int j=indexPath.row;
while (i < [self.tableData count]) {
//get ALL the possible dictionaries and look through them for search matches
NSDictionary *tempDic = [self.tableData objectAtIndex:i];
if ([[tempDic valueForKey:@"activity"] isEqualToString:self.searchResults[j]]) {
self.points = [tempDic valueForKey:@"points"];
break;
}
i++;
}
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
NSString *cellLabel = [[NSString alloc]initWithFormat:@"This activity is worth %@ points", self.points];
cell.detailTextLabel.text = cellLabel;
} else {
cell.textLabel.text = self.tableData[indexPath.row][@"activity"];
cell.detailTextLabel.text = [[NSString alloc]initWithFormat:@"This activity is worth %@ points", self.tableData[indexPath.row][@"points"]];
}
return cell;
}
我尝试在整个代码中添加解释,我希望有人会帮助我解释,因为我从objective-c开始。真的希望这有助于某人,如果有人需要更多的帮助,我会很乐意帮助。