我正在使用以下方法在 UITableView 的部分标题中设置标题,效果很好。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionHeader = nil;
if (section == 0)
{
sectionHeader = nil;
}
if(section == 1)
{
sectionHeader = @"Categories";
}
if (section == 2) {
sectionHeader = @"General";
}
return sectionHeader;
}
使用以下方法,我在部分中设置标题的高度,工作正常。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
但是,一旦我使用以下方法在部分标题中设置背景图像,部分标题中的标题就会消失并变为空,但当然是图像位置。
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"section_background.png"]];
headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 20);
[headerView addSubview:headerImage];
return headerView;
}
如何在节标题中设置标题而不删除其中的背景图像。有没有好心人指出来。提前致谢。
最佳答案
发生这种情况是因为您的 headerView
重叠在你身上页眉标题 .所以最好的方法是......同时添加UILabel
致您的headerView
.并在部分中将标签文本作为标题 os 标题。
我尝试使用以下代码..将其用作您的方式,可能对您的情况有所帮助。
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; //set your image/
UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 205, 15)];//set as you need
headerLbl.backgroundColor = [UIColor clearColor];
headerLbl.text = [self.listOfHeader objectAtIndex:section];
[headerImage addSubview:headerLbl];
headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 20);
[headerView addSubview:headerImage];
return headerView;
}
关于ios - UITableView 部分中的背景图像隐藏部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16055801/