Ios tableview的第一行被搜索栏隐藏

Ios tableview的第一行被搜索栏隐藏,ios,objective-c,uitableview,uisearchbar,Ios,Objective C,Uitableview,Uisearchbar,我在tableview中添加了搜索栏。当我开始搜索时,第一行搜索结果被隐藏。我尝试了下面的方法来纠正它,但它不起作用 self.categoryTableView.contentOffset = CGPointMake(0.0, 44.0); 提前感谢。添加了以下内容,效果很好 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.categoryTableView.frame.size.w

我在tableview中添加了搜索栏。当我开始搜索时,第一行搜索结果被隐藏。我尝试了下面的方法来纠正它,但它不起作用

self.categoryTableView.contentOffset = CGPointMake(0.0, 44.0);

提前感谢。

添加了以下内容,效果很好

UIView *headerView = [[UIView alloc]         initWithFrame:CGRectMake(0,0,self.categoryTableView.frame.size.width, 60)] ;
   [headerView setBackgroundColor:[UIColor clearColor]];
  UISearchBar *txtSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 10,self.categoryTableView.frame.size.width - 20 , 40)];
[headerView addSubview:txtSearchBar]; 
self.categoryTableView.tableHeaderView = headerView;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view;
    if(isFiltered)
    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.categoryTableView.frame.size.width, 45)];
    }
    else
    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    if(isFiltered)
    {
        return 45;
    }
    else
    {
        return 0;
    }
}

添加了以下内容,效果很好

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view;
    if(isFiltered)
    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.categoryTableView.frame.size.width, 45)];
    }
    else
    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    if(isFiltered)
    {
        return 45;
    }
    else
    {
        return 0;
    }
}

您应该首先检查行高是否与单元格的高度相对应。请确保为-tableView:heightForRowAtIndexPath:返回此值,并在-tableView:HeightForHeaderSection:中为标题返回适当的高度,如果搜索栏是标题的一部分。您可以将搜索栏保留在uitableview的标题视图中,也可以设置具有ui搜索栏高度的uitableview的y位置。我的意思是将uitableview放置在场景中uisearchbar的下方,或者您应该首先检查行高是否与单元格的高度相对应。请确保为-tableView:heightForRowAtIndexPath:返回此值,并为-tableView:HeightForHeaderSection:返回适当的页眉高度(如果搜索栏是页眉的一部分)。或者您的搜索栏位于uitableview的标题视图内,或者使用ui搜索栏高度设置uitableview的y位置。我的意思是将uitableview放置在场景中uisearchbar的下方,或者搜索栏未添加到tableview中搜索栏未添加到tableview中