使用搜索栏在UITableView上显示原始UITableView

时间:2013-12-20 12:07:36

标签: ios objective-c uitableview uisearchbar

我正在调整我的应用程序以支持iOS7和6。

我在表格视图上使用搜索栏来显示过滤数据。

但是当我的搜索被过滤时,会在原始数据表视图上方显示带有过滤数据的表格视图,因此它们会重叠。

有没有人有任何想法?

提前致谢。

附上2张图片:

  1. 没有搜索。
  2. enter image description here

    1. 使用搜索(如您所见,表格视图重叠) enter image description here
    2. 编辑:

      添加数据源方法

      #pragma mark - Table view data source
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      // Return the number of sections.
      return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if (tableView == self.searchDisplayController.searchResultsTableView)
      {
          return arregloFiltrado.count;
      }else{
          return arreglo.count;
      }
      }
      
       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          [[NSBundle mainBundle] loadNibNamed:@"CeldaLista" owner:self options:nil];
          cell = celdaLista;
          self.celdaLista = nil;
      }
      
      
      if (tableView == self.searchDisplayController.searchResultsTableView) {
          [(UILabel*)[cell viewWithTag:1] setText: /*Name and surname*/];
          [(UILabel*)[cell viewWithTag:2] setText: /*Other properties*/];
      
          if ([[arregloFiltrado objectAtIndex:indexPath.row] objectForKey:@"photo"] == nil) {
              [(UIImageView*)[cell viewWithTag:4] setImage:/*No image*/];
          }
          else{
              [(UIImageView*)[cell viewWithTag:4] setImage:/*Photo*/];
          }
      } else {
          [(UILabel*)[cell viewWithTag:1] setText:/*Name and surname*/];
          [(UILabel*)[cell viewWithTag:2] setText:/*Other properties*/];
      
          if ([[arreglo objectAtIndex:indexPath.row] objectForKey:@"photo"] == nil) {
              [(UIImageView*)[cell viewWithTag:4] setImage:/*No image*/];
          }
          else{
              [(UIImageView*)[cell viewWithTag:4] setImage:/*Photo*/];
          }
      }
      return cell;
      }
      

      还有什么?

2 个答案:

答案 0 :(得分:1)

试试这段代码:

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView{
        tableView.backgroundColor = [UIColor whiteColor];//color of search result table
    }

答案 1 :(得分:1)

如果您使用UISearchDisplayController,它拥有自己的UITabelview。因此,当您开始搜索搜索显示控制器时,将搜索界面叠加在原始视图控制器的视图上,如下所述:https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchDisplayController_Class/Reference/Reference.html

您可能希望为UISearchDisplayController提供的表设置表格单元格的背景颜色或整个表格视图,使其不透明。

或者,您可以通过实现UISearchBar及其关联的委托方法来过滤原始表:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

  //As the user enters search text filter the array that provides data to the UItableView you wish to filter
}

在此处调查搜索栏代表:https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarDelegate_Protocol/Reference/Reference.html

由于

TG

相关问题