0

我有一個包含兩個部分的UITableView的應用程序。現在我想添加一個UISearchBar以允許搜索。搜索真的工作得很好,並選擇正確的單元格。搜索時,您可以看到有可以選擇的單元格,但標籤是空白的。這裏是我的代碼:UITableView中的單元格在UISearchBar中搜索時保持清晰

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"qrCodeCell"; 
    QRTableViewCell *cell = (QRTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

    if (indexPath.section == 0) { 
     NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"MMM dd, yyyy HH:mm"]; 
     if (tableView == self.searchDisplayController.searchResultsTableView) { 
      //NSLog(@"%@",[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]); 
      [cell.dataLabel setText:[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]]; 
      //NSLog(@"%@",cell.dataLabel.text); 

      NSDate *timeStamp = [[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"timeStamp"]; 
      [cell.timeLabel setText:[format stringFromDate:timeStamp]]; 
     } else { 
      [cell.dataLabel setText:[[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"data"]]; 
      NSDate *timeStamp = [[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"]; 
      [cell.timeLabel setText:[format stringFromDate:timeStamp]]; 
     } 
    } else if (indexPath.section == 1) { 
     NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"MMM dd, yyyy HH:mm"]; 
     if (tableView == self.searchDisplayController.searchResultsTableView) { 
      [cell.dataLabel setText:[[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"data"]]; 
      NSDate *timeStamp = [[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"timeStamp"]; 
      [cell.timeLabel setText:[format stringFromDate:timeStamp]]; 
     } else { 
      [cell.dataLabel setText:[[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"data"]]; 
      NSDate *timeStamp = [[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"]; 
      [cell.timeLabel setText:[format stringFromDate:timeStamp]]; 
     } 
    } 
    return cell; 
} 

這幾乎是完全一樣的問題,因爲這些問題,但這個問題不能得到解決:UISearchBar with UITableView containing custom cells => blank cellsIt doesn't show the contents of the cells in a UITableView filtered by a UISearchDisplayController

我真的很感激,如果有人能幫助我!

編輯:

這裏是我的TableViewController一些代碼:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"data contains[c] %@", searchText]; 
    self.searchResultsQR = [self.qrcodes filteredArrayUsingPredicate:resultPredicate]; 
    self.searchResultsScanned = [self.scannedCodes filteredArrayUsingPredicate:resultPredicate]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 
+0

你返回正確的行號進行搜索時,也證實了在數組中的值,當你搜索? – Harsh

+0

是的,這部分工作完美。正如你可以在我的代碼中看到的,我通過NSLog檢查了它,然後是否已經寫入標籤,但是沒有。 – timo

+0

你可以請同時分享用searchDisplayController代表 – Harsh

回答

0

您的電池未正確註冊。這意味着您的代碼將運行:

cell = [[QRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

它不會創建所有子視圖或連接任何插座。

你需要找出爲什麼你的單元格沒有正確註冊。如果你在故事板中創建它,那麼它應該包含在它將被使用的表格視圖中(它應該自動註冊單元格)。如果您在獨立的XIB文件中創建了它,那麼您需要加載NIB並在加載表視圖時顯式註冊它。

你有2個表格視圖,單元格註冊了一個,但沒有註冊 - 這是原型單元格的工作方式。

你最好的選擇:

-Remove原型細胞,創建一個XIB並與每個表視圖註冊NIB明確

- 未使用搜索控制器(所以你只有1個表視圖,管理搜索欄自己)

These are a few references which you can look forward to.

This is a tutorial for implementing search delgates with storyboard

還發現SO的討論可能給你一些指導,你要去哪裏錯了

Search Bar in UITableView doesn't display text in Cell label

+0

在仔細調試所有內容的時候,我提到過很多都不是這樣(例如突然間電池沒有了)。但是,當您將您的建議外包到一個單獨的XIB文件的單元格時,現在一切都像一種魅力!非常感謝!! – timo

相關問題