我想在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重叠:
最佳答案
您的问题是您的按钮太大。尝试减少它的高度和字体大小,它应该可以工作。您还可以使用以下方法来增加节标题的高度:
- (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;