2012-09-19 47 views
0

我有我的搜索欄有點問題的實現代碼如下的UISearchBar uisearchdisplaycontroller實現代碼如下字幕錯誤

我已經做了的tableview與陣列tableViewArray。
tableViewArray由[text,distance]組成的另一個數組的多行組成。 一切工作正常。
現在我添加了一個searchBar和一個searchdisplaycontroller,它基於一個新的字符串數組(從tableViewArray的「text」對象)進行搜索。 我認爲搜索應該只能用於文本,並且搜索方法在那裏被實現。

現在,當我得到搜索結果,它看起來不錯,搜索返回預期的行。問題是與搜索tableViews副標題。它顯示了tableViewArray的第1,2,3行的距離。

我需要它將距離映射到搜索tableview行中顯示的文本。 我想我需要爲由[文本距離]組成的搜索結果創建一個新的表格視圖數組。文本不是問題,因爲它來自搜索結果,但是如何將新距離映射到舊距離?

使用搜索代表的搜索方法,IM是:

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

而且其中i使搜索的陣列:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

NSLog(@"didUpdateToLocation Location calculator distance array for the tableView"); 

NSMutableArray * distancesInReverseOrder = [[NSMutableArray alloc] init]; 



for (int i = 0; i<allAnnotations.count; i++) { 

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0] ; 
CLLocationCoordinate2D annotationCoord = [[allAnnotations objectAtIndex:indexPath.row] coordinate]; 


CLLocation *location = [[CLLocation alloc] initWithLatitude:annotationCoord.latitude longitude:annotationCoord.longitude]; 

distanceToMe = [newLocation distanceFromLocation:location]; 

[distancesInReverseOrder insertObject:[NSNumber numberWithFloat: distanceToMe] atIndex:0]; 

} 


distances = [[NSMutableArray alloc] initWithArray:[[distancesInReverseOrder reverseObjectEnumerator] allObjects]]; 

// Assuming you have your points on the map in an NSArray called 
// allAnnotations and your distances in distances, create a 
// new mutable array to hold both 
tableViewArray = [[NSMutableArray alloc] init]; 

// Iterate over all of the points, and add a new element to the mutable 
// array which is a new array containing a point and its distance 
for (int i = 0; i < allAnnotations.count; i++) { 
    NSArray *newItem = [NSArray arrayWithObjects: [allAnnotations objectAtIndex: i], [distances objectAtIndex: i], nil]; 
    [tableViewArray addObject: newItem]; 
} 

// Now, sort the new array based upon the distance in the second element 
// of each array (ie, the distance). 
[tableViewArray sortUsingComparator: ^(id obj1, id obj2) { 
    NSNumber *dist1 = [obj1 objectAtIndex:1]; 
    NSNumber *dist2 = [obj2 objectAtIndex:1]; 

    return [dist1 compare:dist2]; 
}]; 

searchResults = [NSMutableArray arrayWithCapacity:[tableViewArray count]]; 




searchItems = [[NSMutableArray alloc] init]; 

for (int i = 0; i < allAnnotations.count; i++) { 
    NSArray *newItem = [NSArray arrayWithObjects: [[[tableViewArray objectAtIndex:i] objectAtIndex:0] subtitle], @"bla", nil]; 
    [searchItems addObject: newItem]; 
} 

/* 
for (int i=0; i<tableViewArray.count; i++) { 
    [searchItems insertObject:[[[tableViewArray objectAtIndex:i] objectAtIndex:0] subtitle] atIndex:0]; 
} 
*/ 

NSLog(@"searchitems count is %i", searchItems.count); 

[tableView reloadData]; 

[locationManager stopUpdatingLocation]; 

}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
/* 
// Update the filtered array based on the search text and scope. 
// Remove all objects from the filtered search array 
[searchResults removeAllObjects]; 
// Filter the array using NSPredicate 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText]; 

// SearchResult所= [NSMutableArray的arrayWithArray:[[ tableViewArray objectAtIndex:1] filteredArrayUsingPredicate:predicate]];

*/

/* 

SearchResult所= [[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); 
+0

是否將您的searchresults數組中的對象複製到您的tableview數組? –

+0

我該怎麼做? searchResults數組完全由tableview數組的索引0組成。所以根本不考慮指數1(距離)。 –

回答

2

如果我正確理解你的問題,你有被用作表視圖數據源2個獨立的陣列,我們姑且稱之爲textArraydistanceArray

現在您根據搜索字符串篩選textArray,並且您需要「相應」篩選distanceArray。要做到這一點

一種方法是用indexesOfObjectsPassingTest更換filteredArrayUsingPredicate,因爲返回一組的匹配的索引,可以同時應用於陣列:

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

現在你可以使用filteredTextArrayfilteredDistanceArray作爲數據源搜索表格視圖。

或者,如果數組中的每個對象都是例如包含一行的文本和距離的字典,則可以使用單個數組作爲數據源。

更新:據我現在瞭解,您的tableViewArray的每個項目是包含2項(一個用於文本和一個距離)的數組。

在這種情況下,我將建議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的項目。

這簡化了cellForRowAtIndexPath中的事情,例如,

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]; 
} 
+0

你好,我使用相同的數組作爲tableview的數據源。但是這個數組是你提到的兩個數組的組合。 tableview數組的索引0來自locationArray,索引1來自距離數組。我基本上搜索位置數組,在這種情況下是「searchitems」(位置數組的副本)。所以tableViewArray是(索引0位置,索引1距離)。 在你寫的代碼中,你在哪裏定義了實際搜索的內容? (我會同時嘗試你的解決方案,我認爲這幾乎是我認爲必須得到的答案) 順便說一下,謝謝:) –

+0

@ B-Man:我的代碼搜索'textArray'數組中的'searchText'。 - 我還不知道你的陣列是如何工作的。如果你顯示你的'cellForRowAtIndexPath:'代碼,這可能會有所幫助。 - 或者你先試試我的解決方案,並回答如果你有更多問題......就像你想的那樣。 –

+0

上面更新的原始代碼。請看一看。謝謝! :) –

相關問題