gpt4 book ai didi

ios - UIView 是 tableView 中的隐藏部分标题

转载 作者:行者123 更新时间:2023-11-28 20:08:10 25 4
gpt4 key购买 nike

我想在 tableView 部分标题上显示一个按钮:我正在使用 UIView 托管按钮,但它与部分标题重叠。这是 viewForHeaderInSection 方法:

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

static NSString *header = @"customHeader";

UITableViewHeaderFooterView *vHeader;

id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];

NSString *tmp = [theSection name];
NSLog(@"SECTIONNAME = %@",tmp);

vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
NSLog(@"SECTION = %ld",(long)section);

if (!vHeader) {
vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
vHeader.textLabel.textColor = [UIColor whiteColor];

vHeader.contentView.backgroundColor = [UIColor redColor];
}

if (section == 0) {
vHeader.textLabel.backgroundColor = [UIColor redColor];
vHeader.textLabel.textColor = [UIColor whiteColor];


vHeader.contentView.backgroundColor = [UIColor redColor];
}

else if (section == 1) {
vHeader.textLabel.backgroundColor = [UIColor orangeColor];
vHeader.textLabel.textColor = [UIColor blueColor];

vHeader.contentView.backgroundColor = [UIColor orangeColor];
}
else if (section == 2) {
vHeader.textLabel.backgroundColor = [UIColor yellowColor];
vHeader.textLabel.textColor = [UIColor blueColor];

vHeader.contentView.backgroundColor = [UIColor yellowColor];
}
else if (section == 3) {
vHeader.textLabel.backgroundColor = [UIColor magentaColor];
vHeader.textLabel.textColor = [UIColor whiteColor];

vHeader.contentView.backgroundColor = [UIColor magentaColor];
}
else if (section == 4) {
vHeader.textLabel.backgroundColor = [UIColor blueColor];
vHeader.textLabel.textColor = [UIColor whiteColor];

vHeader.contentView.backgroundColor = [UIColor blueColor];
}
else if (section == 5) {
vHeader.textLabel.backgroundColor = [UIColor darkGrayColor];
vHeader.textLabel.textColor = [UIColor whiteColor];

vHeader.contentView.backgroundColor = [UIColor darkGrayColor];
}

vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];


UIView *mView = [[UIView alloc]initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 44.0f)];
[mView setBackgroundColor:[UIColor clearColor]];

UIImageView *logoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 20, 20)];
[logoView setImage:[UIImage imageNamed:@"carat.png"]];
[mView addSubview:logoView];

UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(210, 0, 150, 30)];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[bt setTag:section];
[bt.titleLabel setFont:[UIFont systemFontOfSize:20]];
[bt.titleLabel setTextAlignment:NSTextAlignmentCenter];
[bt.titleLabel setTextColor:[UIColor blackColor]];
[bt setTitle: @"More Info" forState: UIControlStateNormal];
[bt addTarget:self action:@selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[mView addSubview:bt];
return mView;

return vHeader;

}

我应该怎么做才能保留部分标题并显示按钮。节标题在 titleForHeaderInSection 方法中定义?

编辑:

带有正常部分标题的屏幕截图

enter image description here

隐藏部分标题的屏幕截图,UIVIew 重叠:

enter image description here

最佳答案

你的问题是你的按钮太大了。尝试减小它的高度和字体大小,它应该可以工作。您还可以使用以下方法增加节标题的高度:

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

编辑:你可以像这样实现你的行为:

//create the regular section header
if (regularHeader) {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 28)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 5, 300, 20)];
titleLabel.text = title;
[headerView addSubview:titleLabel];
return headerView;
}

//create the header with the more button
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(210, 0, 50, 30)];
moreButton.titleLabel.text = @"More";
[headerView addSubview:moreButton];
return headerView;

关于ios - UIView 是 tableView 中的隐藏部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294375/

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