【问题标题】:why view for header in section does not appear properly when the tableview style is grouped in iOS, objective c当tableview样式在iOS中分组时,为什么节中的标题视图没有正确显示,目标c
【发布时间】:2016-08-18 05:39:00
【问题描述】:

我正在使用一个包含四个部分的表格视图。对于每个部分,我都实现了一个标题视图。当我将tableview style 用作Plain 时,它可以正常工作。但是如果我使用tableview style 作为Grouped 它看起来是有线的。

这就是我实现tableview 委托方法的方式。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else if(section == 1)
    {
        return 1;
    }
    else if (section == 2)
    {
        return 3;
    }
    else
    {
        return 1;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 50)];
    UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, screenSize.width-80, 50)];

    if (section == 0)
    {
        titlelabel.text = @"Flight Summary";
        [headerView addSubview:titlelabel];
        return headerView;
    }
    else if(section == 1)
    {
        titlelabel.text = @"Price Summary";
        [headerView addSubview:titlelabel];
        return headerView;
    }
    else if (section == 2)
    {
        titlelabel.text = @"Traveller Details";
        [headerView addSubview:titlelabel];
        return headerView;
    }
    else
    {
        titlelabel.text = @"Book Now";
        [headerView addSubview:titlelabel];
        return headerView;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"checkcell"];
    return cell;
}

这是我使用tableview样式为Plain时的截图

图片链接:image with plain style

这是我使用tableview样式为Grouped时的截图

图片链接:image with grouped stye

我不知道发生了什么,希望您能帮忙。

【问题讨论】:

  • 为什么这只发生在Grouped tableview style
  • 你实现tableView:heightForHeaderInSection:方法了吗?
  • 不,我没有实现,因为大多数时候我使用Plain 样式并且不需要该方法。 @rmaddy
  • 阅读 tableView:viewForHeaderInSection: 的文档。它明确指出您必须实现tableView:heightForHeaderInSection:。不管是普通表还是分组表。
  • k 明白了,谢谢@rmaddy

标签: ios objective-c uitableview


【解决方案1】:

需要设置节标题高度。试试下面的方法,希望对你有帮助。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多