当结果为零时,UISearchDisplayController多个分隔线

时间:2013-01-27 19:40:53

标签: ios uitableview uisearchdisplaycontroller

使用UISearchDisplayController我在this URL上传的图片中发现了以下问题。

行的默认高度为74,但当结果为零时,我不会发生,但会出现多个分隔线。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     return 74;
}

   -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self performSelectorInBackground:@selector(loadMySearchObjectsInBackground) withObject:nil];  
    return NO;
}

- (void) loadMySearchObjectsInBackground{
    NSString *searchStr=self.searchDisplayController.searchBar.text;
    NSArray *foundItems;
    .....Code to fetch Result from Server.......
    [self.searchDisplayController.searchResultsTableView reloadData];
}

当结果> 0时,上面的代码工作正常。但是当foundItems count = 0时会导致问题。

任何想法都可以解决这个问题。

2 个答案:

答案 0 :(得分:2)

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
   if ([self.searchDataArray count] == 0) needSeparateCell = NO;
   else needSeparateCell = YES:
}

在numberOfRowsInSection和cellForRowAtIndexPath中设置separatorSyteNone

if (tableView == self.searchDisplayController.searchResultsTableView){
    if(!needSeparateCell) {
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    } else {
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    }
}

答案 1 :(得分:2)

如果您不需要tableFooterView,有一种非常简洁的方法可以实现此目的。

self.searchDisplayController.searchResultsTableView.tableFooterView = [UIView new];

这将删除最后一个单元格下面的所有分隔符。因此,如果tableView为空,则根本不会有分隔线。

它适用于任何类型的UITableView。