gpt4 book ai didi

ios - ViewForHeaderInSection 代码仅适用于 header 出列

转载 作者:行者123 更新时间:2023-11-29 03:14:19 25 4
gpt4 key购买 nike

我有自定义的 tableView header ,但是我指定 header 文本颜色和分隔线的代码仅在 header 出列时才有效,当我将它们滚动到屏幕外时。只有这样他们才会返回白色文本颜色和可见的分隔符。我的代码有什么问题,即 header 在第一次加载时没有以正确的状态返回?

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"FIRST HEADER";
} else if (section == 1) {
return @"SECOND HEADER";
} else {
return @"THIRD HEADER";
}
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeaderFooterView *myHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];

if (!myHeaderView) {

myHeaderView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
}
myHeaderView.textLabel.textColor = [UIColor whiteColor];

CGFloat leftMargin = 0.0;
CGFloat lineWidth = 1.0;

UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, myHeaderView.frame.size.height - lineWidth, myHeaderView.frame.size.width - leftMargin, lineWidth)];
separatorLine.backgroundColor = [UIColor whiteColor];

[myHeaderView addSubview:separatorLine];

return myHeaderView;
}

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

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {

((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
}
}

最佳答案

原因是您的 header 帧在出队前等于 CGRectZero。这意味着,该 header 将来会调整大小,您需要为分隔符设置适当的自动调整掩码。然后,分隔符也将调整大小。

UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, 0, 0, lineWidth)];
separatorLine.backgroundColor = [UIColor redColor];
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

[header.contentView addSubview:separatorLine];

关于ios - ViewForHeaderInSection 代码仅适用于 header 出列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21853323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com