2013-02-08 61 views
0

我正在嘗試在UITableview中進行搜索。我已經以正確的方式實現了UISearchDisplayDelegate,UISearchBarDelegate方法。這是我的cellForRowAtIndexPath的樣子。正在搜索UITableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    if (tableView == self.searchDisplayController.searchResultsTableView){ 
     Contact *contact = [self.filteredListContent objectAtIndex:indexPath.row]; 

     NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName]; 
     NSLog(@"CellForRowAtIndexPath contact text is %@",text); 
     cell.textLabel.text = text; 


     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 


    }else{ 
    NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]]; 

    //---get all states beginning with the letter--- 
    NSPredicate *predicate = 
    [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@",alphabet]; 
    NSArray *contacts = [listContent filteredArrayUsingPredicate:predicate]; 
    Contact *contact = [contacts objectAtIndex:indexPath.row]; 

    NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName]; 
    cell.textLabel.text = text; 


     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 

    return cell; 

} 

這是我filterContentForSearchText方法

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    [self.filteredListContent removeAllObjects]; // First clear the filtered array. 

    for (Contact *contact in listContent) 
    { 
     NSString *searchString = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName]; 
     NSRange range = [searchString rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
     if (range.location != NSNotFound) { 
      [self.filteredListContent addObject:contact]; 
      [self.searchDisplayController.searchResultsTableView reloadData]; 
     } 
    } 
} 

奇怪的是。在我的cellForRowAtIndexPath它返回我正確的數據。但tableview本身不斷給我沒有結果標籤。

對此有何幫助?

+0

[這是你在找什麼!(http://www.iphonesdkarticles.com/2009/01/uitableview -searching-table-view.html) – Hemang

+0

我跟着一個更好的教程,然後。但仍然有問題。 – Steaphann

回答

0
  1. 使用數組表顯示數據(showDataArray)
  2. 使用一個數組來存儲所有的輸入值(dataArray中)
  3. 使用showDataArray來填充你的表總是
  4. 在searchfield時性格特徵範圍改變調用從dataArray中
  5. 過濾使用謂詞的數據的方法
  6. 保存的值來showDataArray
  7. 來電錶reloadData

編碼快樂:)

0

按照本教程how-to-add-search-bar-uitableview ..

剛纔看到的方法的cellForRowAtIndexPath:,其中,當用戶從搜索記錄的UISearchBar如何設置搜索陣列...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"RecipeCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; 
    } else { 
     cell.textLabel.text = [recipes objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

我希望這對你有幫助...

+0

謝謝你的回答!你能看看這個問題嗎? http://stackoverflow.com/questions/14768859/how-to-acces-the-right-navigation-controller-inside-storyboard/14769261#comment20677346_14769261 – Steaphann

0

首先添加UISearchBar on top of UITabelView documentation

,然後採取兩個NSMutableArray一個陣列中諸如此類ViewDidLoad方法添加到另一個陣列

self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1 
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObjects:@"YorArrayList", nil]; // array no - 2 

[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; // add 2array to 1 array 

和寫入以下的UISearchBar

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText 
{ 
     NSString *name = @""; 
     NSString *firstLetter = @""; 

    if (self.listOfTemArray.count > 0) 
     [self.listOfTemArray removeAllObjects]; 

     if ([searchText length] > 0) 
     { 
       for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1) 
       { 
         name = [self.ItemOfMainArray objectAtIndex:i]; 

         if (name.length >= searchText.length) 
         { 
           firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])]; 
           //NSLog(@"%@",firstLetter); 

           if([firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame) 
           { 
            // strings are equal except for possibly case 
            [self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]]; 
            NSLog(@"=========> %@",self.listOfTemArray); 
           } 
         } 
       } 
     } 
     else 
     { 
      [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ]; 
     } 

     [self.tblView reloadData]; 
} 
的委託方法

並寫入cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"]; 

     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
     cell.textLabel.textColor = [UIColor blackColor]; 
    } 

     cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row]; 

    return cell; 
} 

此代碼可能有助於您...謝謝:)