自定义 UITableView 标题部分 [英] Customize UITableView header section
问题描述
我想为每个部分自定义 UITableView
标题.到目前为止,我已经实现了
I want to customize UITableView
header for each section. So far, I've implemented
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
这个 UITabelViewDelegate
方法.我想要做的是获取每个部分的当前标题,然后将 UILabel
添加为子视图.
this UITabelViewDelegate
method. What I want to do is to get current header for each section and just add UILabel
as a subview.
到目前为止,我无法做到这一点.因为,我找不到任何东西来获取默认部分标题.第一个问题,有没有办法获得默认的section header?
So far, I'm not able to accomplish that. Because, I couldn't find anything to get default section header. First question,is there any way to get default section header?
如果不可能,我需要创建一个容器视图,它是一个 UIView
但是,这次我需要设置默认背景颜色、阴影颜色等.因为,如果你仔细查看部分的标题,它已经自定义了.
If it's not possible, I need to create a container view which is a UIView
but,this time I need to set default background color,shadow color etc. Because, if you look carefully into section's header, it's already customized.
如何为每个部分标题获取这些默认值?
How can I get these default values for each section header?
推荐答案
你可以试试这个:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string =[list objectAtIndex:section];
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
这篇关于自定义 UITableView 标题部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!