Ios 仅为一个uitableview设置标题

Ios 仅为一个uitableview设置标题,ios,xcode,uitableview,Ios,Xcode,Uitableview,我需要帮助。我在一个UIView中有三个uitableview。但是,我只希望一个UITableView具有标题。我怎么能用密码说呢?我已经有了下面的代码。谢谢各位 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (tableView == Table1) { return @"My Header Text"; }

我需要帮助。我在一个
UIView
中有三个
uitableview
。但是,我只希望一个
UITableView
具有标题。我怎么能用密码说呢?我已经有了下面的代码。谢谢各位

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {   
    if (tableView == Table1) {
        return @"My Header Text";
    } else if (tableView == Table2) {
        return nil;
    } else if (tableView == Table3) {
        return nil;
    }
}   

如果这不起作用,您也可以尝试将收割台高度返回零:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == Table1) 
    {
        return 50.0; // or what's the height?
    } 
    else
    {
         return 0.0;
    }
}

如果这不起作用,您也可以尝试将收割台高度返回零:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == Table1) 
    {
        return 50.0; // or what's the height?
    } 
    else
    {
         return 0.0;
    }
}

您提供的代码将仅为某个节添加标题视图。因此,如果您有两个或更多节,则需要编写额外的逻辑来设置每个节的标题视图

这里是为单个表设置
tableHeaderView
的简单方法

UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = @"my table header";
table1.tableHeaderView = label;

您提供的代码将仅为某个节添加标题视图。因此,如果您有两个或更多节,则需要编写额外的逻辑来设置每个节的标题视图

这里是为单个表设置
tableHeaderView
的简单方法

UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = @"my table header";
table1.tableHeaderView = label;
试试这个

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
 UILabel *label = [[[UILabel alloc] init] autorelease];
 label.frame = CGRectMake(20, 6, 300, 30);
 label.textColor = [UIColor blackColor];
 label.font = [UIFont boldSystemFontOfSize:16];
 label.text = sectionTitle;

  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
  [view addSubview:label];
  return view;
  }
试试这个

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
 UILabel *label = [[[UILabel alloc] init] autorelease];
 label.frame = CGRectMake(20, 6, 300, 30);
 label.textColor = [UIColor blackColor];
 label.font = [UIFont boldSystemFontOfSize:16];
 label.text = sectionTitle;

  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
  [view addSubview:label];
  return view;
  }

请发送您的UITableView代码请发送您的UITableView代码检查R.A的答案(+1),这样更容易解决您的问题。但是,我如何才能使它保持在原来的位置。我的意思是,当我滚动时它会拖行。@user2234264这会起作用,但不太好。我刚刚为提供的代码和级别生成了一个解决方案。这样更容易复制粘贴。但如果我正确理解情况肯定不好。@user2234264您希望它始终可见(并以某种方式连接到滚动,即它与单元格一起滚动,但不会离开屏幕)?然后是单个tableView部分和该部分的标题。TableView的标题不会总是出现在屏幕上。请检查R.A的答案(+1),这样可以更好地解决您的问题。但是,我怎样才能使它保持在它的位置上呢?我的意思是,当我滚动时,它会拖行。@user2234264这会起作用,但不太好。我刚刚为提供的代码和级别生成了一个解决方案。这样更容易复制粘贴。但如果我正确理解情况肯定不好。@user2234264您希望它始终可见(并以某种方式连接到滚动,即它与单元格一起滚动,但不会离开屏幕)?然后是单个tableView部分和该部分的标题。TableView的标题不会始终显示在屏幕上。这在iOS 5+中不起作用,因为“在iOS 5.0之前,对于TableView:ViewForHeaderSection:返回的视图为零的节,表视图会自动将标题高度调整为0。在iOS 5.0及更高版本中,必须使用此方法返回每个节标题的实际高度。”这在iOS 5+中不起作用,因为“在iOS 5.0之前,对于tableView:viewForHeaderInSection:返回零视图的分区,表视图会自动将标题高度调整为0。在iOS 5.0及更高版本中,您必须在此方法中返回每个分区标题的实际高度。”