Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c uisearchbar uisearchdisplaycontroller表格视图字幕错误_Objective C_Ios_Xcode_Searchbar_Searchdisplaycontroller - Fatal编程技术网

Objective c uisearchbar uisearchdisplaycontroller表格视图字幕错误

Objective c uisearchbar uisearchdisplaycontroller表格视图字幕错误,objective-c,ios,xcode,searchbar,searchdisplaycontroller,Objective C,Ios,Xcode,Searchbar,Searchdisplaycontroller,我的tableview搜索栏有点问题 我已经用数组tableViewArray创建了一个tableview。 tableViewArray由另一个数组的许多行组成,该数组由[text,distance]组成。 一切正常。 现在我添加了一个searchBar和一个searchdisplaycontroller,它们基于一个新的字符串数组(来自tableViewArray的“text”对象)进行搜索。 我认为搜索应该只对文本可用,搜索方法就是在这个基础上实现的 现在,当我得到搜索结果时,它看起来不错

我的tableview搜索栏有点问题

我已经用数组tableViewArray创建了一个tableview。
tableViewArray由另一个数组的许多行组成,该数组由[text,distance]组成。 一切正常。
现在我添加了一个searchBar和一个searchdisplaycontroller,它们基于一个新的字符串数组(来自tableViewArray的“text”对象)进行搜索。 我认为搜索应该只对文本可用,搜索方法就是在这个基础上实现的

现在,当我得到搜索结果时,它看起来不错,搜索返回预期的行。问题在于“搜索表视图”子标题。它显示了tableViewArray第1、2、3行的距离

我需要它将距离映射到搜索表视图行中显示的文本。 我想我需要为搜索结果创建一个新的表视图数组,该数组由[text distance]组成。文本不是问题,因为它来自搜索结果,但如何将新距离映射到旧距离

我在搜索代理中使用的搜索方法是:

searchResults = [[NSArray alloc]init];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",
                            searchText];
searchResults = [searchItems  filteredArrayUsingPredicate:resultPredicate];
希望有人能帮忙:)提前谢谢

原代码:

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{

    if (theTableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];
    } else {
        return [tableViewArray count];
    }
}  

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:     UITableViewCellStyleSubtitle
                                  reuseIdentifier: CellIdentifier] ;

     if (theTableView == self.searchDisplayController.searchResultsTableView) {

    } else {
         cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier     forIndexPath:indexPath];
}

    // Configure the cell...
    // cell.textLabel.numberOfLines = 0;
   // cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

    if (theTableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [[[tableViewArray objectAtIndex:indexPath.row]     objectAtIndex:0] subtitle];
}

    cell.detailTextLabel.textColor = [UIColor redColor];




float blabla=  [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
if (blabla < 1000) {
         cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f m",blabla];
} else {
         cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f km",blabla/1000];
}





NSString *text = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
NSRange q8RangeValue = [text rangeOfString:@"Q8" options:NSCaseInsensitiveSearch];
NSRange okRangeValue = [text rangeOfString:@"OK" options:NSCaseInsensitiveSearch];

if (q8RangeValue.length >0  ) {
        cell.imageView.image = [UIImage imageNamed:@"q8.png"];
} else if (okRangeValue.length >0 ) {
        cell.imageView.image = [UIImage imageNamed:@"OK logo.png"];

} else {
        cell.imageView.image = nil;
}

return cell;
}
//searchResults=[NSMutableArray arrayWithArray:[[tableViewArray objectAtIndex:1]filteredArrayUsingPredicate:predicate]]

*/

searchResults=[[NSArray alloc]init]

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

    searchResults =[searchItems filteredArrayUsingPredicate:resultPredicate];


*/

// Create index set of all objects in textArray that contain searchText:
NSIndexSet *set = [searchItems indexesOfObjectsPassingTest:
                   ^BOOL(NSString *text, NSUInteger idx, BOOL *stop) {
                       NSRange range = [text rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
                       return (range.location != NSNotFound);
                   }];

// Filter textArray:
filteredTextArray = [searchItems objectsAtIndexes:set];
// Filter distanceArray:
filteredDistanceArray = [distances objectsAtIndexes:set];

NSLog(@"filtered text array is %@", filteredTextArray);
NSLog(@"filtered distance array is %@",filteredDistanceArray);

如果我正确理解了您的问题,那么您有两个单独的数组用作表视图的数据源,我们将它们称为
textary
distanceArray

现在,根据搜索字符串过滤
textary
,需要对
distanceArray
进行“相应”过滤

一种方法是将
filteredArrayUsingPredicate
替换为
indexesOfObjectsPassingTest
,因为这将返回一组可应用于两个数组的匹配索引:

// Create index set of all objects in textArray that contain searchText:
NSIndexSet *set = [textArray indexesOfObjectsPassingTest:
    ^BOOL(NSString *text, NSUInteger idx, BOOL *stop) {
        NSRange range = [text rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
        return (range.location != NSNotFound);
    }];

// Filter textArray:
filteredTextArray = [textArray objectsAtIndexes:set];
// Filter distanceArray:
filteredDistanceArray = [distanceArray objectsAtIndexes:set];
现在,您可以使用
filteredTextArray
FilteredInstanceArray
作为搜索表视图的数据源

或者,如果数组中的每个对象都是包含一行文本和距离的字典,则可以使用单个数组作为数据源

更新:据我现在所知,您的
tableViewArray
的每个项目都是一个包含两个项目的数组(一个用于文本,一个用于距离)

在这种情况下,我建议直接过滤
tableViewArray

NSIndexSet *set = [tableViewArray indexesOfObjectsPassingTest:
                   ^BOOL(NSArray *item, NSUInteger idx, BOOL *stop) {
                       NSString *subtitle = [[item objectAtIndex:0] subtitle];
                       NSRange range = [subtitle rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
                       return (range.location != NSNotFound);
                   }];

searchResults = [tableViewArray objectsAtIndexes:set];
现在,
searchResults
是过滤后的数组,每个项目的结构都与
tableViewArray
中的项目相同

这简化了
cellforrowatinexpath
中的内容,例如

if (theTableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [[[searchResults objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
     distance = [[[searchResults objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
} else {
    cell.textLabel.text = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
    distance = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
}

您是否将searchresults数组中的对象复制到了您的tableview数组?我该怎么做?searchResults数组仅由tableview数组的索引0组成。所以它根本没有考虑索引1(距离)。但是这个数组是您提到的两个数组的组合。tableview数组的索引0来自locationArray,索引1来自距离数组。我基本上是在location数组上进行搜索,在本例中是“searchitems”(locaion数组的副本)。因此tableViewArray是(索引0位置,索引1距离)。在您编写的代码中,您在何处定义实际搜索的内容?(我会同时尝试你的解决方案,我想这几乎就是我所认为的答案)顺便说一句,谢谢:@B-Man:My code在数组
textArray
中搜索
searchText
-我还不明白你的阵列是如何工作的。如果您显示
cellforrowatinexpath:
-或者你先试试我的解决方案,如果你有更多的问题就回答。。。正如你所希望的。@B-Man:请看我的最新答案,我希望它能有所帮助。否则,欢迎您再次提问,但我可能不会在明天之前回复。谢谢,这看起来太棒了。我会调查一下,明天再给你回复:)晚安:)
// Create index set of all objects in textArray that contain searchText:
NSIndexSet *set = [textArray indexesOfObjectsPassingTest:
    ^BOOL(NSString *text, NSUInteger idx, BOOL *stop) {
        NSRange range = [text rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
        return (range.location != NSNotFound);
    }];

// Filter textArray:
filteredTextArray = [textArray objectsAtIndexes:set];
// Filter distanceArray:
filteredDistanceArray = [distanceArray objectsAtIndexes:set];
NSIndexSet *set = [tableViewArray indexesOfObjectsPassingTest:
                   ^BOOL(NSArray *item, NSUInteger idx, BOOL *stop) {
                       NSString *subtitle = [[item objectAtIndex:0] subtitle];
                       NSRange range = [subtitle rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
                       return (range.location != NSNotFound);
                   }];

searchResults = [tableViewArray objectsAtIndexes:set];
if (theTableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [[[searchResults objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
     distance = [[[searchResults objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
} else {
    cell.textLabel.text = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
    distance = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
}