2012-07-23 63 views
2

我有一個表格視圖與搜索欄設置。一切都很好:當我在搜索欄中輸入文本時,結果將根據我的搜索文本在表格視圖中更新。我遇到的問題是在確定表格單元的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]; 
    } 
} 
+0

例如我考慮使用'NSUInteger指數= [self.grapes indexOfObject:STR];',不同之處在於它只會返回最低索引,我知道至少對於搜索詞Cabernet,將會有4個我需要返回的ID。 – lemontwist 2012-07-23 17:26:41

回答

1

在你做了選擇項目的方法,從那個被選中,並通過您的數組進行迭代,直到找到匹配的項目單元格中的文本,然後打開電池的詳細視圖。如果您擁有所有獨特的商品名稱,這隻會起作用。

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *str = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text]; 
    for (int j=0; j<whateverArray.count; j++) 
    { 
     NSString *temp = [whateverArray objectAtIndex:j]; 
     if ([str isEqualToString:temp]) 
     { 
      //do whatever you would do if the user actually clicked the row at position j 
     } 
    } 
} 
+0

是的!一個燈泡剛剛在我腦海中熄滅!謝謝! – lemontwist 2012-07-23 17:29:48

+0

更新了回答,代碼 – Dustin 2012-07-23 17:33:41

+0

Thx,這太好了。 :-D – lemontwist 2012-07-23 17:35:01

0

您還可以爲與陣列計數相關的每個單元格創建一個順序標籤號。然後,您抓住標籤號碼並將其用於索引處的對象。

這比搜索每個字符串的效率稍高一些,它可以容納重複的字符串名稱。

代碼將是:

- (UITableViewCell *) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionCellIdentifier]; 
cell.tag = i //where i increments by one each entry into the function. 
} 
+0

我看不出這將如何幫助確定我在searchResult數組中查找的對象。 – lemontwist 2012-07-26 16:53:31

+0

考慮與數組直接相關的每個單元格的標籤。如果你願意的話,1比1。 示例[數組objectforindex:1]等於cell.tag = 1.當您在函數didSelectRowAtIndexPath中時,您將獲得單元格。標記值應該直接與數組中的索引相關聯,並使用正確的值。涼? – 2012-07-27 19:11:20

相關問題