3

我正在使用分组表,并使用 tableView: viewForHeaderInSection: 方法自定义部分中的标题并使用 tableView: heightForHeaderInSection: 设置高度。

我创建了一个视图并在其中放置了一个标签,如下所示:

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


    // Create a custom title view.
    UIView *ctv;
    UILabel *titleLabel;

    // Set the name.
    {...} // Code not relevant

    // If an iPhone.
    if ([Config isPhone]) {
        ctv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 36)];
    }
    // If an iPad
    else {
        ctv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 75)];
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 544, 55)];
    }

    // Config the label.
    {...} // Code not relevant

    // Center the items.
    ctv.center = CGPointMake(tableView.center.x, ctv.center.y);
    titleLabel.center = ctv.center;

    // Add the label to the container view.
    [ctv addSubview:titleLabel];

    // Return the custom title view.
    return ctv;


}

在您旋转屏幕之前,这一切都很好。位置已关闭。我意识到这是因为视图在另一个方向上被居中,导致中心的计算不再正确。解决方案应该是添加约束。我尝试在下面添加约束:

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(ctv);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[ctv]|"
                         options:0
                         metrics:nil
                         views:viewsDictionary
                       ];
[tableView addConstraints:constraints];

但是当我尝试下面的方法时,我发现没有父视图与之关联,这是完全有道理的,因为从技术上讲,它并没有被添加到返回的视图中。所以我想我会尝试以这种方式添加约束:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:ctv
                                     attribute:NSLayoutAttributeCenterX
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:tableView
                                     attribute:NSLayoutAttributeCenterX
                                    multiplier:1.0
                                      constant:0
                                      ];
[tableView addConstraint:constraint];

但这也有错误。我尝试将 tableView 变量切换到全局表属性,但结果相同。我还试图弄清楚如何在视图中添加约束确实加载方法但它失败了,因为我无法弄清楚如何从表对象返回到表的节标题。我最后的想法是在约束中设置表格的宽度,并设置一个使整个表格居中。这个过程有效,但现在我的应用程序中间有一个丑陋的滚动条,当它处于横向时。所以问题是,在加载单个节标题以添加此约束后,我在哪里/如何访问它们?我对 Objective-C 还是很陌生,因此感谢您的帮助。

***** 基于 rdelmar 建议的新代码 ****

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *ctv = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"groupHeader"];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 544, 55)];
    [titleLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:ctv
                                        attribute:NSLayoutAttributeCenterX
                                        relatedBy:0
                                          toItem:titleLabel
                                        attribute:NSLayoutAttributeCenterX
                                       multiplier:1.0
                                         constant:0
                                      ];
    [ctv addConstraints:@[constraint]];
    titleLabel.text = @"string";
    [ctv addSubview:titleLabel];
    return ctv;
}

但就像我提到的那样,它给了我一个“约束必须包含第一个布局项”的错误。

4

2 回答 2

1

我在最近的一个项目中这样做是为了添加标签和 UIActivityIndi​​catorView:

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0, 250, 20)];
    [label setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:label attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeLeft relatedBy:0 toItem:label attribute:NSLayoutAttributeLeading multiplier:1 constant:-10];
    [headerView addConstraints:@[con1,con2]];
    label.text = @"Pick an Activity from the List";
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [spinner setTranslatesAutoresizingMaskIntoConstraints:NO];
    if (activityIndicatorShouldStop == NO) [spinner startAnimating];
    [headerView addSubview:spinner];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:spinner attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeRight relatedBy:0 toItem:spinner attribute:NSLayoutAttributeTrailing multiplier:1 constant:10];
    [headerView addConstraints:@[con3,con4]];
    return headerView;
}
于 2013-01-30T20:44:58.537 回答
0

如果你不能让约束起作用,你的原始代码加上一个自动调整大小的掩码(灵活的左右边距)就可以完成这项工作。

一个更简单的解决方案是返回一个 UILabel 作为标题视图,并带有居中的文本。

您第一次尝试约束是行不通的,因为您设置错误。

表视图负责设置表头视图的框架。您需要担心标签标题中的位置。对此的 VFL 将是"|titleLabel|"- 标题标签的大小应与其父视图(标题视图)相匹配。

于 2013-01-30T21:42:42.323 回答