Ios 在不丢失章节标题的情况下更改UITable章节背景色
我已更改了tableviews部分的背景颜色/背景图像。Ios 在不丢失章节标题的情况下更改UITable章节背景色,ios,title,background-color,sectionheader,Ios,Title,Background Color,Sectionheader,我已更改了tableviews部分的背景颜色/背景图像。 我使用的是普通桌面视图。 不用担心,使用此代码可以轻松工作: -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease]; sectio
我使用的是普通桌面视图。 不用担心,使用此代码可以轻松工作:
-(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;
}
现在的问题是,章节标题不见了。
我用以下代码设置章节标题:
- (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];
}
}
需要你的帮助。非常感谢 编辑1: 多亏了Mutix: 就我而言,答案是:
-(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;
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分{
UIView*sectionView=[[UIView alloc]initWithFrame:CGRectMake(0,0,728,20)]自动释放];
sectionView.backgroundColor=[UIColor GREENCLOR];
//sectionView.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage ImageName:@“whiteBackground.png”];
//将标签添加到视图
UILabel*titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,728,20)];
titleLabel.backgroundColor=[UIColor clearColor];
//章节标题
if([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
titleLabel.text=@”;
}否则{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
titleLabel.text=[sectionInfo name];
}
[截面视图添加子视图:标题标签];
[标题标签释放];
返回剖面视图;
}查看headerinsection会覆盖
标题为headerinsection
的方法
要在使用viewForHeaderInSection
时向页眉添加标题,您需要向章节页眉视图添加标签,如下所示:
-(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;
}
只需添加
[titleLabel setBackgroundColor:[UIColor clearColor]]代码>
因为在我的例子中,标签与绿色重叠。~最佳解决方案iPhone 3.5和4屏幕~
根据此代码设置背景色或背景图像。在这里,您不必计算截面高度,只需设置为xib
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.sectionHeaderHeight)];
sectionView.backgroundColor = [UIColor greenColor];
return sectionView;
}
从iOS6开始,我们有了更好的解决方案:
有一个委托方法
-(void)tableView:(UITableView*)tableView
willDisplayHeaderView:(UIView*)视图
章节:(NSInteger)章节
你可以这样使用这个方法
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
//Set the background color of the View
view.tintColor = [UIColor blueColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
}
从UITableViewDelegate和所需的ui视图实现以下方法
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UITableViewHeaderFooterView()
view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
return view
}
如果需要更改页脚,请在…viewForFooterInstition.中使用类似的代码。。方法
要绘制自己的字体,请使用类似的自定义:
...
let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
label.font = UIFont(name: "HelveticaNeue", size: 14)!
label.text = self.tableView(tableView, titleForHeaderInSection: section)
view.contentView.addSubview(label)
view.textLabel.hidden = true
...
从iOS开始,我们可以使用-(void)tableView:(UITableView*)tableView将displayHeaderView:(UIView*)视图用于section:(NSInteger)节。我已经在下面解释了我的答案。澄清:这是从iOS6开始的
...
let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
label.font = UIFont(name: "HelveticaNeue", size: 14)!
label.text = self.tableView(tableView, titleForHeaderInSection: section)
view.contentView.addSubview(label)
view.textLabel.hidden = true
...