2

嗨,我试图在 tableview 标题上设置自定义视图,但自定义视图不适合标题。

自定义视图如下图所示,在此图像中,自定义视图为橙色,标题视图为灰色。但我希望自定义视图充满标题视图。

在此处输入图像描述

请帮忙。

我的代码:

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

        UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)];
        header = [[HeaderView alloc] initWithFrame:CGRectMake(sectionView.frame.origin.x, sectionView.frame.origin.y, sectionView.frame.size.width, sectionView.frame.size.height)];
        [sectionView addSubview:header];
        sectionView.tag=section;
        sectionView.backgroundColor = [UIColor grayColor];

        UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
        [sectionView addGestureRecognizer:headerTapped];

        return  sectionView;
    }
4

5 回答 5

3

如果您使用 Xib 将自定义视图加载为节标题,则执行如下代码:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *headerview = [[[NSBundle mainBundle] loadNibNamed:@"SecHeader"
                                                            owner:self
                                                          options:nil] objectAtIndex:0];
    headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 80);

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [headerview addGestureRecognizer:headerTapped];

    return headerview;
}
于 2016-07-21T11:14:54.383 回答
1

您可以使用UITableViewHeaderFooterView.Create your custom view作为UITableViewHeaderFooterView. 使用适当的约束。现在将此视图用作标题视图。

YourHeaderView *YourHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YourHeaderViewIdentifier"];
于 2016-07-21T06:25:09.587 回答
1

尝试使用自动布局

[header setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(header);
NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[header]-|" options:0 metrics:nil views:views];
NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[header]-|" options:0 metrics:nil views:views];
[sectionView addConstraints:horizontalConstraints];
[sectionView addConstraints: verticalConstraints];

您的代码将如下所示

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

        UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)];
        [header setTranslatesAutoresizingMaskIntoConstraints:NO];
        header = [[HeaderView alloc] init];
        [sectionView addSubview:header];

        NSDictionary *views = NSDictionaryOfVariableBindings(header);
        NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[header]-|" options:0 metrics:nil views:views];
        NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[header]-|" options:0 metrics:nil views:views];

        [sectionView addConstraints:horizontalConstraints];
        [sectionView addConstraints: verticalConstraints];

        sectionView.tag=section;
        sectionView.backgroundColor = [UIColor grayColor];

        UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
        [sectionView addGestureRecognizer:headerTapped];

        return  sectionView;
    }
于 2016-07-21T06:25:57.047 回答
0

在 viewForHeaderInSection

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *labelSection;
UIView  *viewSection = [[UIView alloc]init];
viewSection.frame = CGRectMake(0, 0, tableview.frame.size.width, 20);
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20);

[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = @"section title";
labelSection.text = name;
[viewSection setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName"]]];
[labelSection setTextColor:[UIColor whiteColor]];
[viewSection addSubview:labelSection];
return viewSection;
}
于 2016-07-21T06:24:39.663 回答
0

如果您使用故事板,则创建一个视图作为表格视图单元格

  1. 拖放2个tableview单元格
  2. 将一个单元格设计为标题视图,其他单元格将用作表格视图列表单元格。并给出每个单元格子视图的自动布局。

在此处输入图像描述

现在从如下界面为每个单元格提供不同的单元格标识符

在此处输入图像描述

现在在 tableview 的标题视图委托中使用视图中的这个单元格。

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *identifier = @"SectionHeader";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    return cell;
}

希望能帮助到你!

于 2016-07-21T10:42:45.430 回答