我想在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
方法中定义的吗?
编辑:
具有正常部分标题的屏幕截图
隐藏部分标题的屏幕截图,UIVIew重叠:
答案 0 :(得分:3)
你的问题是你的按钮太大了。尝试减少它的高度和字体大小,它应该工作。您还可以使用以下方法增加节标题的高度:
- (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;