我的应用中有标签,用户可以在其中执行搜索。在用户输入关键字和tableView填充结果后,会有很多空行。我已经"清洁"不必要的行(把它变成[UIColor clearColor]
),但现在我有一个空的空间,问题没有解决。我想要的是,只是一个填充了搜索结果的表,没有空格,没有行(如果有空格,当桌面上只有几个结果时,它完全没问题。但是我得到了这个"空的&#34 ;当有很多结果的时候,我必须向下滚动才能看到所有结果。在这种情况下,我不希望在最后一个单元格之后出现任何内容。有一个代码,我用来创建UISearch和tableView,它很简单:
对于UITableView:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"number of rows is ? %d", self.filteredArray.count);
return [self.filteredArray count];
}
- (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];
}
NSString *_cellText = self.filteredArray[indexPath.row];
cell.textLabel.text = _cellText;
return cell;
}
对于UISearch:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
self.filteredArray = [NSMutableArray new];
self.searchResults = [NSArray new];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
self.searchResults = [self._placeOfObjects filteredArrayUsingPredicate:resultPredicate];
NSLog(@" metod called ");
NSLog(@"searchResults is %@", self.searchResults);
// if (self.searchResults){
// _filteredName = ((placeHolder *)(self.searchResults)).name;
// NSLog(@"HOLY Grale %@", _filteredName);
// }
// NSLog(@" What user search is %@", ((placeHolder *)(self.searchResults.name));
if (self.searchResults.count > 0){
// _filteredName = ((placeHolder *)(self.searchResults[0])).name;
for (int i=0; i<[self.searchResults count]; i++) {
NSLog(@" counto %d", [self.searchResults count]);
_filteredName = ((PlaceHolder *)(self.searchResults[i])).name;
[self.filteredArray addObject:_filteredName];
}
}
else {
NSLog(@"array is empty");
}
NSLog(@"HOLY WATER %@", _filteredName );
NSLog(@" %@ holy array", self.filteredArray);
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView;
{
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 1)];
footer.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:footer];
}
任何建议都将不胜感激,谢谢!