UIsearch栏不会将数据返回到表

编辑过的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; if (isFiltered) { int rowCount=indexPath.row; Aves *filtrada=[filteredTableData objectAtIndex:rowCount]; cell.textLabel.text=filtrada.name; NSLog(@"mostrando: "); }else { int rowCounter=indexPath.row; Aves *author=[theauthors objectAtIndex:rowCounter]; cell.textLabel.text=author.name; } NSLog(@"mostrando: "); return cell; 

}

 -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text { if(text.length == 0) { isFiltered = FALSE; } else { isFiltered = true; int i; [filteredTableData removeAllObjects]; for(i=0;[theauthors count]>i;i++) { Aves *name=[theauthors objectAtIndex:i]; //NSLog(name.name); NSRange nameRange = [[name.name lowercaseString] rangeOfString:[text lowercaseString]]; if(nameRange.length>0) { [filteredTableData addObject:name]; NSLog(name.name); } } [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; } } 

编辑:经过一段时间我解决了一些问题。刚更新我的代码,问题是重新绘制tableView,其他一切都没问题。 检查它并给出你有任何想法plz ^^

再次感谢你的时间。

我假设你正在使用原型细胞? 我在其中一个项目中遇到了类似的问题。

当显示搜索结果并且调用tableView:cellForRowAtIndexPath: ,表视图在属于搜索结果控制器的表中传递,而不是主表视图。 问题是,搜索结果表视图对表的原型单元格一无所知,因此dequeueReusableCellWithIdentifier:返回nil。 但是只是分配/初始化UITableCellView不会给你一个原型单元格,所以你在故事板中布置的UI都不存在。

修复很简单:在tableView:cellForRowAtIndexPath: ,不要调用dequeueReusableCellWithIdentifier: on tableview传入; 只需在主表视图中调用它。 所以基本上,只需改变这个:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

对此:

 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

没有必要进行零检查; Apple的Storyboards发行说明说:

dequeueReusableCellWithIdentifier:方法保证返回一个单元格(前提是您已定义具有给定标识符的单元格)。 因此,不需要使用“检查方法的返回值”模式,就像之前典型的tableView实现一样:cellForRowAtIndexPath:。

如果(nameRange.location == 0)你的应用程序是否会遇到此代码?

更改代码段

 else { isFiltered = true; int i=0; [filteredTableData removeAllObjects]; filteredTableData = [[NSMutableArray alloc] init]; //for (Aves* name in theauthors) for(i=0;[theauthors count]>i;i++) { Aves *name=[theauthors objectAtIndex:i]; NSRange nameRange = [name.foodName rangeOfString:text ]; if(nameRange.location ==0) { [filteredTableData addObject:name]; } [self.tableView reloadData]; } } 

 else { isFiltered = true; int i=0; [filteredTableData removeAllObjects]; //filteredTableData = [[NSMutableArray alloc] init]; not needed //for (Aves* name in theauthors) for(i=0;[theauthors count]>i;i++) { Aves *name=[theauthors objectAtIndex:i]; NSRange nameRange = [name.foodName rangeOfString:text ]; if(nameRange.location != NSNotFound) { [filteredTableData addObject:name]; } } [self.tableView reloadData]; } 

用这个:

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list. if (isFiltered) { return [filteredTableData count]; } else { return [theauthors count]; } } 

替换方法:

  -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text { if(text.length == 0) { isFiltered = FALSE; } else { isFiltered = true; int i=0; [filteredTableData removeAllObjects]; filteredTableData = [[NSMutableArray alloc] init]; //for (Aves* name in theauthors) for(i=0;[theauthors count]>i;i++) { Aves *name=[theauthors objectAtIndex:i]; NSRange nameRange = [[name.foodName lowercaseString] rangeOfString:[text lowercaseString]]; if(nameRange.length>0) { [filteredTableData addObject:name]; [self.tableView reloadData]; } } } } 

终于修好了。 这是我的工作代码,大家你=)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; if (isFiltered==TRUE) { int rowCount=indexPath.row; //for (rowCount=0; rowCount<[filteredTableData count]; rowCount++) { Aves *filtrada=[filteredTableData objectAtIndex:rowCount]; cell.textLabel.text=filtrada.name; //} }else if(isFiltered==FALSE) { int rowCounter=indexPath.row; Aves *author=[theauthors objectAtIndex:rowCounter]; cell.textLabel.text=author.name; } return cell; } 

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

  [filteredTableData removeAllObjects]; filteredTableData=[[NSMutableArray alloc]init ]; if(text.length == 0) { isFiltered = FALSE; } else { isFiltered = TRUE; int i; for(i=0;[theauthors count]>i;i++) { Aves * filtrado=[[Aves alloc]init]; filtrado=[theauthors objectAtIndex:i]; //NSLog(filtrado.name); NSRange nameRange = [[filtrado.name lowercaseString] rangeOfString:[text lowercaseString]]; if(nameRange.length>0) { [filteredTableData addObject:filtrado]; } } [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil 

waitUntilDone:NO]; }}

你可能想要替换

 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

在你的cellForRowAtIndexPath中

 UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

原因是你无论如何都没有使用出列的单元格。