更改UITable部分backgroundColor而不丢失部分标题 [英] Change UITable section backgroundColor without loosing section Title
问题描述
我更改了tableviews部分的backgroundColor / backgroundImage。
我正在使用普通的tableView。
不用担心,使用此代码可以顺利运行:
i have changed the backgroundColor/backgroundImage of my tableviews sections.
I am using plain tableView.
No worries, get worked without problems with this code:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];
//For using an image use this:
//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];
return sectionView;
}
现在问题是,部分标题消失了。
我使用以下代码设置该部分的标题:
I am setting the title of the section with this code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
return @"";
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
}
我得到了部分标题,如果我不使用
i am getting the section titles if i do not use
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
需要你的帮助。
非常感谢。
Need your help.
Thanks a lot.
编辑1:
感谢Mutix:
在我的情况下,答案是:
in my case, the answer would be:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 20)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];
//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];
//Add label to view
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 20)];
titleLabel.backgroundColor = [UIColor clearColor];
//section Title
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
titleLabel.text = @"";
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
titleLabel.text = [sectionInfo name];
}
[sectionView addSubview:titleLabel];
[titleLabel release];
return sectionView;
}
推荐答案
viewForHeaderInSection
覆盖 titleForHeaderInSection
方法。
要在使用 viewForHeaderInSection
时为标题添加标题,您需要在标题页面视图中添加标签,如下所示:
To add a title to your header when using viewForHeaderInSection
you will need to add a label to the section header view like so:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];
//Add label to view
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)];
titleLabel.text = @"title";
[sectionView addSubview:titleLabel];
[titleLabel release];
return sectionView;
}
这篇关于更改UITable部分backgroundColor而不丢失部分标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!