我有一個表格視圖與搜索欄設置。一切都很好:當我在搜索欄中輸入文本時,結果將根據我的搜索文本在表格視圖中更新。我遇到的問題是在確定表格單元的ID是什麼時候傳遞給我的細節視圖。澄清:表視圖中的單元格通常從SQLite3數據庫生成並按其名稱排序。所以如果我有4個單元格:(Cabernet Cortis,Cabernet Franc,Cabernet Sauvignon和Ruby Cabernet),他們的ID通常是(78,79,80,378)。然而,當我搜索「赤霞珠」,並且這四個項目出現時,indexPath.row是(1,2,3,4)而不是(78,79,80,378),所以它將我帶到錯誤的葡萄中詳細視圖(這是按照輸入到我的數據庫中的順序排列的前四個葡萄)。如何找出我在搜索過程中點擊了哪些UITableViewCell?
下面是一些代碼,以幫助清理事情。我認爲,如果我可以獲得每個出現搜索結果的葡萄的ID並且具有這些值的MSMutableArray,那麼將會有所幫助,然後使用[actualIDNumber objectAtIndex:indexPath.row]
而不僅僅是indexPath.row
。
//ViewGrapesViewController.h
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
- (void) searchGrapesTableView;
@property (nonatomic, retain) NSMutableArray *listOfGrapes;
@property (nonatomic, retain) NSMutableArray *searchResult;
@property (nonatomic, retain) NSMutableArray *searchResultID;
@property (nonatomic) Boolean isSearchOn;
//ViewGrapesViewController.m
//synthesize variables, etc
- (void)viewDidLoad
{
//obtain grapes from the DB, etc.
//add searchNames to an NSMutableArray
listOfGrapes = [[NSMutableArray alloc] init];
for (int i=1; i<=[self.grapes count]; i++) {
Grapes *theGrape = [self.grapes objectAtIndex:(i-1)];
NSString *grapeSearchName = theGrape.searchName;
[listOfGrapes addObject:grapeSearchName];
}
searchResult = [[NSMutableArray alloc] init];
searchResultID = [[NSMutableArray alloc] init];
isSearchOn = NO;
//search bar stuff
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
[super viewDidLoad];
}
- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearchOn = YES;
}
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length] > 0) {
isSearchOn = YES;
[self searchGrapesTableView];
}
else {
isSearchOn = NO;
}
[self.tableView reloadData];
}
- (void) searchGrapesTableView {
[searchResult removeAllObjects];
[searchResultID removeAllObjects];
for (NSString *str in listOfGrapes)
{
NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0) {
[searchResult addObject:str];
//would love to add ID to searchResultID array here, don't know how to do that
}
}
}
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self searchGrapesTableView];
}
- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar {
isSearchOn = NO;
[self.searchBar resignFirstResponder];
[self.tableView reloadData];
}
//table view code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//first check if the user is searching
if (isSearchOn) {
return 1;
}
//if not, then search by alpha order
else {
return 24;
}
}
//create header titles & number of rows in section (it's a lot of code and works fine)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"grapeCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//is the user searching?
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
//otherwise...
else {
NSInteger theNumber;
NSInteger numberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue];
theNumberToAdd = numberToAdd;
theNumber = numberToAdd + indexPath.row;
//cell title = grape name
Grapes *grape = [self.grapes objectAtIndex:theNumber];
NSString *theGrapeName = grape.grapeName;
cell.textLabel.text = theGrapeName;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//is the user searching?
if (isSearchOn) {
theNumberToAdd=0; //which is wrong but here for the time being
//i have to have the ID here in order to do some logic to figure out what row to return
}
//otherwise...
else {
theNumberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue]; // i performed this logic elsewhere
}
ViewGrapeViewController *detailViewController = (ViewGrapeViewController *)self.presentedViewController;
detailViewController.detailItem = [self.grapes objectAtIndex: (indexPath.row+theNumberToAdd)];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"toGrapeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//is the user searching
if (isSearchOn) {
theNumberToAdd=0; // which is wrong
}
//otherwise...
else {
theNumberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue];
}
NSDate *object = [_objects objectAtIndex:(indexPath.row+theNumberToAdd)];
[[segue destinationViewController] setDetailItem:object];
}
}
例如我考慮使用'NSUInteger指數= [self.grapes indexOfObject:STR];',不同之處在於它只會返回最低索引,我知道至少對於搜索詞Cabernet,將會有4個我需要返回的ID。 – lemontwist 2012-07-23 17:26:41